Equivalent syntax to Python regex functions

I am converting a python code which is similar to Python re.sub() function by Regex.replaceAllIn

The result string (e.g. val replacement_string) after replacement looks like (?P\d+) (?P\w+)

When trying to search (similar to Python’s re.search(reg, input) ) with Java’s pattern function


Scala REPL throws java.util.regex.PatternSyntaxException: Unknown inline modifier near index 2

Is (?P<…>…) a valid regex in Scala? Or how to achieve the effect as described in the stack overflow?


As far as I know the ?P<> is not supported in Scala/Java.

The last time I was converting a regex from python to scala I used this useful website to test my regex to make sure it is compatible in my chosen language:


This stackoverflow solutution seems to have the java equivalent to what you are after:

I found that Java only accept following syntax (without P embedded)


Then exploiting with replaceAllIn can achieve the effect I am looking for.