seudo
seudo

Reputation: 108

How to check if a given Java Regex is valid in Python

I want to check if the Regex defined in Java is valid by Python.

But the Regex in the two languages are little different.

For example, to parse the character dot (.)

"\."  # Python version
"\\." # Java version

Is there any way to check Java regex in Python?

Upvotes: 0

Views: 197

Answers (1)

Dima
Dima

Reputation: 40510

Regex are the same in java and python. The difference you pointed out is due to the way java compiler handles string constants. Backslash has special meaning as an escape character, so, to include a backslash itself into a String literal, you have to repeat it twice. Thus "\\." to express a string constant "\."

Try this: System.out.println(Pattern.compile("\\.").toString());

Upvotes: 1

Related Questions