Regular expressions - does Scala not have modifiers? If so ... why?

540 views
Skip to first unread message

Brent Gracey

unread,
Jun 16, 2015, 11:49:13 AM6/16/15
to scala...@googlegroups.com
Hi all; 

I'm having a look at Regular Expression in scala - and wondering if I'm missing what the equivalent of 'modifiers' (from PHP  or Java) are?

From https://regex101.com/#pcre it gives a Modifier quick reference (top right) based on the Flavor selected; top middle on the Left.

My googleing turned up

http://stackoverflow.com/questions/17930774/scala-regex-ignorecase
where the (?i) enables case insensitive matching

http://stackoverflow.com/questions/1088554/scala-regex-enable-multiline-option
(?s) is key here in regard to matching against multi-line strings


But the PHP and Java (my understanding is  http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#Field Detail is relatively equivalent) modifiers seem to be a longer list.

So after that pre-amble

1) Am I correct in my understanding that Scala Regex doesn't have a suite of modifiers?
2) If so; is there a reason for this? Are modifiers just 'syntax sugar' that can be implemented in Regex expressions without using special cases?


Thanks

Simon Ochsenreither

unread,
Jun 16, 2015, 12:15:19 PM6/16/15
to scala...@googlegroups.com
Scala regexes are basically Java regexes. Everything which is supported on the Java version you are running, also works in Scala.

Som Snytt

unread,
Jun 16, 2015, 1:29:37 PM6/16/15
to Brent Gracey, scala-user
It's a weakness of the JavaDoc that it's difficult to correlate the inline flags with the flags argument.

(?idmsuxU-idmsuxU) are inline flags.

A couple of flag arg values have no inline option:

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CANON_EQ

The Scala API doesn't accommodate the flag args. I don't know if there's a great reason for that.

scala> :pa -raw
// Entering paste mode (ctrl-D to finish)

package scala.util.matching { object X { def x(p: java.util.regex.Pattern) = new Regex(p) } }

// Exiting paste mode, now interpreting.


scala> val r = util.matching.X.x(Pattern.compile("abc", Pattern.CASE_INSENSITIVE))
r: scala.util.matching.Regex = abc

scala> "ABC" match { case r(_*) => }





--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brent Gracey

unread,
Jun 17, 2015, 2:05:26 AM6/17/15
to scala...@googlegroups.com
Hi -

Thanks for the pointers that I am actually pretty close to the Java libraries; that should help me going forward.

I tried the below in the repl and got

scala> :pa -raw
// Entering paste mode (ctrl-D to finish)


package scala.util.matching { object X { def x(p: java.util.regex.Pattern) = new Regex(p) } }


// Exiting paste mode, now interpreting.


<console>:1: error: illegal start of definition
       
package scala.util.matching { object X { def x(p: java.util.regex.Pattern) = new Regex(p) } }
       
^



And in my code I gave


package utils
import java.util.regex.Pattern
import scala.util.matching.Regex


object RegexHelper {

 
def applyThat(regexExp: String, context: String) = {

 val myPattern
: Pattern = java.util.regex.Pattern.compile("abc", java.util.regex.Pattern.CASE_INSENSITIVE)
 val r
= X.x(myPattern)


 
"ABC" match { case r(_*) => }

 
}
}

object X { def x(p: java.util.regex.Pattern) = new Regex(p) }

Which gives a compile error

type mismatch;
 found
: java.util.regex.Pattern
 required
: String


object X { def x(p: java.util.regex.Pattern) = new Regex(p) }


Which I think has to do with not being in the correct namespace for my object X definition?



Som Snytt

unread,
Jun 17, 2015, 10:47:49 AM6/17/15
to Brent Gracey, scala-user
Looks like you're using a scala version from a time long, long ago.

Try 2.11, if you can, to exploit the library internals.

I'm going to update my scala script to set the prompt to include the version...

Brent Gracey

unread,
Jun 17, 2015, 11:09:51 AM6/17/15
to scala...@googlegroups.com
Ah; thanks for helping me through; I am indeed on a slightly historic version of our fav language.
Reply all
Reply to author
Forward
0 new messages