Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala regex find and replace

Tags:

regex

scala

I'm having problems finding and replacing portions of a string using regex in scala.

Given the following string: q[k6.q3]>=0 and q[dist.report][0] or q[dist.report][1] and q[10]>20 I want to replace all the occurrences of "and" and "or" with "&&" and "||".

The regex I have come up with is: .+\s((and|or)+)\s.+. However, this seems to only find the last "and".

When using https://regex101.com/#pcre I tried to solve this by adding the modifiers gU, which seems to work. But I'm not sure how to use those modifiers in Scala code.

Any help is much appreciated

like image 574
ulejon Avatar asked Dec 30 '25 01:12

ulejon


1 Answers

Why not to use solution like:

str.replaceAll("\\sand\\s", " && ").replaceAll("\\sor\\s", " || ")
like image 92
Nyavro Avatar answered Dec 31 '25 17:12

Nyavro