Scala Regex enable Multiline option

后端 未结 3 1138
滥情空心
滥情空心 2020-12-08 11:05

I\'m learning Scala, so this is probably pretty noob-irific.

I want to have a multiline regular expression.

In Ruby it would be:

MY_REGEX = /         


        
3条回答
  •  心在旅途
    2020-12-08 11:43

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

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

    And that will return

    A
    B
    c
    

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

    Or in this case:

    def matchNode( value : String ) : Boolean = {
    
        """(?s).*().*""".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 use of the word new in code worldwide. ;)

提交回复
热议问题