Scala multiline regex / regular expression (?m) not working -
Scala multiline regex / regular expression (?m) not working -
(all of illustration can run through scala repl or sbt console)
i have multiline string:
val a= "a0da000043\n"+ "a020000008933f57845f706921\n"+ "a0a0000000 (9000)\n"+ "a0a40000027fde (9fxx,9000) \n"+ "84e400000bd2012f261c86bc6c3c679f (6101,9000)\n"+ "00a4040008a000000151000000 (61xx,9000)"
then have regular expression:
(?m)^ *(?:[0-9a-f]{2})+(?: +\([0-9a-fx,]+\))? *$
i utilize this:
scala> """(?m)^ *(?:[0-9a-f]{2})+(?: +\([0-9a-fx,]+\))? *$""".r.pattern.matcher(a).matches() res10: boolean = false
but if this:
scala> ("""(?m)^ *(?:[0-9a-f]{2})+(?: +\([0-9a-fx,]+\))? *$""".r findallin a).tolist res8: list[string] = list(a0da000043, a020000008933f57845f706921, a0a0000000 (9000), "a0a40000027fde (9fxx,9000) ", 84e400000bd2012f261c86bc6c3c679f (6101,9000), 00a4040008a000000151000000 (61xx,9000))
it returns lines. wonder why .matches() not working.
if seek single line in .matches
scala> """(?m)^ *(?:[0-9a-f]{2})+(?: +\([0-9a-fx,]+\))? *$""".r.pattern.matcher("a0a40000027fde (9fxx,9000)").matches() res9: boolean = true
now simplest multi line regex not working
"(?m)^foo$".r.pattern.matcher("foo\nfoo").matches() res38: boolean = false
what wrong? can give illustration on how utilize (?m) or seek match val a.
i want know if val lines matches regex.
thanks in advance
edit: tried of reply below, utilize (?s) , .* newline. worked illustration 1 considering invalid info valid. ex:
scala> val c = "a0a0000000 (9000" c: string = a0a0000000 (9000 scala> """(?s)^ *(?:[0-9a-f]{2})+(?: +\([0-9a-fx,]+\))? *.*$""".r.pattern.matcher(c).matches() res45: boolean = true
because matches
tries match whole string. "(?m)^foo$"
regex won't match newline character , next foo
string nowadays in input string foo\nfoo
.
the below code homecoming true
, because regex used, match whole string.
"(?s)^foo.*$".r.pattern.matcher("foo\nfoo").matches()
regex scala pattern-matching multiline
Comments
Post a Comment