[ACCEPTED]-Scala Regex enable Multiline option-multiline

Accepted answer
Score: 43

This is a very common problem when first 16 using Scala Regex.

When you use pattern matching 15 in Scala, it tries to match the whole string, as 14 if you were using "^" and "$" (and did not 13 activate multi-line parsing, which matches 12 \n to ^ and $).

The way to do what you want 11 would be one of the following:

def matchNode( value : String ) : Boolean = 
  (ScriptNode findFirstIn value) match {    
    case Some(v) => println( "found" + v ); true    
    case None => println("not found: " + value ) ; false
  }

Which would 10 find find the first instance of ScriptNode 9 inside value, and return that instance as v 8 (if you want the whole string, just print 7 value). Or else:

val ScriptNode =  new Regex("""(?s).*<com:Node>.*""")
def matchNode( value : String ) : Boolean = 
  value match {    
    case ScriptNode() => println( "found" + value ); true    
    case _ => println("not found: " + value ) ; false
  }

Which would print all all 6 value. In this example, (?s) activates dotall 5 matching (ie, matching "." to new lines), and 4 the .* before and after the searched-for 3 pattern ensures it will match any string. If 2 you wanted "v" as in the first example, you 1 could do this:

val ScriptNode =  new Regex("""(?s).*(<com:Node>).*""")
def matchNode( value : String ) : Boolean = 
  value match {    
    case ScriptNode(v) => println( "found" + v ); true    
    case _ => println("not found: " + value ) ; false
  }
Score: 5

Just a quick and dirty addendum: the .r method 6 on RichString converts all strings to scala.util.matching.Regex, so you can 5 do something like this:

"""(?s)a.*b""".r replaceAllIn ( "a\nb\nc\n", "A\nB" )

And that will return

A
B
c

I 4 use this all the time for quick and dirty 3 regex-scripting in the scala console.

Or 2 in this case:

def matchNode( value : String ) : Boolean = {

    """(?s).*(<com:Node>).*""".r.findAllIn( text ) match {

       case ScriptNode(v) => System.out.println( "found" + v ); true    

       case _ => System.out.println("not found: " + value ) ; false
    }
}

Just my attempt to reduce the 1 use of the word new in code worldwide. ;)

Score: 5

Just a small addition, use tried to use 6 the (?m) (Multiline) flag (although it might 5 not be suitable here) but here is the right 4 way to use it:

e.g. instead of

val ScriptNode =  new Regex("""<com:Node>?m""")

use

val ScriptNode =  new Regex("""(?m)<com:Node>""")

But again 3 the (?s) flag is more suitable in this question 2 (adding this answer only because the title 1 is "Scala Regex enable Multiline option")

More Related questions