Reputation: 756
I want to use Regex non-capturing groups in scala, briefly worded "?:".
After hours of testing various cases I came here to search for a solution. I found this question and its answer, but it didn't worked for me. Is it possible to make non-capturing groups work in scala regexes when pattern matching
So I wrote a minimal example to test the statement of the thread above.
val test = ("""(?:<.*>)(.*)(?:<.*>)""".r findFirstIn ("<test>hello</test>")) getOrElse ""
println("DEBUG MESSAGE (test): " + test)
Expected Output: hello
Real Output: <test>hello</test>
So what is wrong with my code?
Upvotes: 2
Views: 758
Reputation: 31035
Try with this regex instead:
<.*?>(.*?)<.*?>
Scala code
Also, try this code to get the content of capturing groups #1 like this:
val string = "<test>hello</test>"
val pattern = """<.*?>(.*?)<.*?>""".r
pattern.findAllIn(string).matchData foreach {
m => println(m.group(1))
}
Upvotes: 2