Scala regex improvement => accept flags for compile pattern

409 views
Skip to first unread message

Léonard Schneider

unread,
Jun 20, 2012, 10:03:19 AM6/20/12
to scala...@googlegroups.com
Hi,

Just a remark about Scala Regex. The java Pattern is directly compiled, without giving the possibility to pass flags, such as MULTILINE to support multiline regular expressions. Hence, I have to subclass regex to use it for my use case.

It would be nice to add an optional parameter in the Regex constructor in later scala releases ;)

Best regards,


Leo

Léonard Schneider

unread,
Jun 20, 2012, 10:31:23 AM6/20/12
to scala...@googlegroups.com
Arrgh, I can not even subclass it, because pattern is a val, not a def ;(

Dinotzar Tzar

unread,
Jun 20, 2012, 10:31:23 AM6/20/12
to scala...@googlegroups.com
Leo,

I think you can embed the flags directly into your regex string:
 
  "(?m)quack".r


Check out the Java documentation.

D.

Léonard Schneider

unread,
Jun 20, 2012, 10:52:57 AM6/20/12
to scala...@googlegroups.com
Hi Dinotzar,


Anyway, I'll know one more thing. Sorry for the noise.

BR

Leo

On Wednesday, June 20, 2012 4:31:23 PM UTC+2, Dinotzar wrote:

Nils Kilden-Pedersen

unread,
Jun 20, 2012, 12:12:26 PM6/20/12
to Léonard Schneider, scala...@googlegroups.com
On Wed, Jun 20, 2012 at 9:31 AM, Léonard Schneider <leonard....@gmail.com> wrote:
Arrgh, I can not even subclass it, because pattern is a val, not a def ;(

You can override a val. Something like this should work:

class MyRegex(override val pattern: java.util.regex.Pattern) extends util.matching.Regex(pattern.pattern)

Som Snytt

unread,
Jun 20, 2012, 1:39:45 PM6/20/12
to Léonard Schneider, scala...@googlegroups.com
Noise is good.  Though the sound of crickets chirping can also be pleasant on summer nights.

Acknowledging the good work someone did on the Scaladoc:
http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.util.matching.Regex

It seems the bit about flags was deemed important enough to document out of the gate (in older scaladoc).

Simple demo follows. It would be nice if Scaladoc had a "copy example to clipboard" button.

I especially like the doc: "Regex does not provide a method that returns a Boolean."  Because, of course, you're thinking, what tells me if it matches, t/f?  So maybe it would be nice to be able to search/sort scaladoc by result type.

package myregex

import scala.util.matching._
import scala.util.matching.Regex.Match
import java.util.regex.Pattern
import java.util.regex.Pattern._

class MyRegex(r: String, gs: String*) extends Regex(r, gs: _*) {
  override val pattern = Pattern.compile(r, CASE_INSENSITIVE | MULTILINE)
}

object Test {
  val months = Map(1 -> "Jan", 2 -> "Feb", 3 -> "Mar",
                   4 -> "Apr", 5 -> "May", 6 -> "Jun",
                   7 -> "Jul", 8 -> "Aug", 9 -> "Sep",
                   10 -> "Oct", 11 -> "Nov", 12 -> "Dec")
  def main(args: Array[String]) {
    val dateP2 = new MyRegex("""(\d\d\d\d)-(\d\d)-(\d\d)""", "year", "month", "day")
    val dateP3 = new MyRegex("""JUL (\d\d), (\d\d\d\d)""", "day", "year")
    def reformatDate(text: String) = dateP2 replaceAllIn ( text, (m: Match) =>
      "%s %s, %s" format (months((m group "month").toInt), m group "day", m group "year")
    )
    def inJuly(text: String) = (dateP3 findFirstIn text).nonEmpty
    val j = reformatDate("2011-07-15")
    println(s"${j} ${inJuly(j)}")

Daniel Sobral

unread,
Jun 20, 2012, 1:54:11 PM6/20/12
to Som Snytt, Léonard Schneider, scala...@googlegroups.com
On Wed, Jun 20, 2012 at 2:39 PM, Som Snytt <som....@gmail.com> wrote:
>
> Simple demo follows. It would be nice if Scaladoc had a "copy example to
> clipboard" button.

I wish it had a "open a window with simple-scala, paste and run it".

--
Daniel C. Sobral

I travel to the future all the time.

Dinotzar Tzar

unread,
Jun 20, 2012, 2:09:01 PM6/20/12
to scala...@googlegroups.com
On Wed, Jun 20, 2012 at 7:54 PM, Daniel Sobral <dcso...@gmail.com> wrote:
On Wed, Jun 20, 2012 at 2:39 PM, Som Snytt <som....@gmail.com> wrote:
>
> Simple demo follows. It would be nice if Scaladoc had a "copy example to
> clipboard" button.

I wish it had a "open a window with simple-scala, paste and run it".


A wish Scaladoc is the kind of Scaladoc where you have two slices of Scaladoc and you wish you had a REPL.

(Apologies to http://youtu.be/jYyBZE0kBtE )

Philippe Lhoste

unread,
Jun 22, 2012, 8:27:46 AM6/22/12
to scala...@googlegroups.com
On 20/06/2012 16:52, L�onard Schneider wrote:
> Thanks a lot. I was looking
> at http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html where it is
> not mentioned. I should have looked
> at http://docs.oracle.com/javase/tutorial/essential/regex/pattern.html

Funny how people still look at Java 1.4.2 docs. Perhaps because Google links to them as
top results as they have been around so long.

At least http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html shows this
information:
(?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off

:-)

That's a useful feature, as it allows people to input regexes with these flags directly
available without additional UI.

--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --



Reply all
Reply to author
Forward
0 new messages