0

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?

4
  • did you want to check for this particular regex? Commented Dec 11, 2014 at 3:47
  • regex to match literal dot in both languages [.] Commented Dec 11, 2014 at 3:52
  • OP is not looking to just parse the dot it seems. Commented Dec 11, 2014 at 3:56
  • Go to regex101.com.Select python and put your regex there.That's d best way Commented Dec 11, 2014 at 4:17

1 Answer 1

1

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());

Sign up to request clarification or add additional context in comments.

1 Comment

i think you need to escape the backslash one more time in python like java when the regex is not defined as raw string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.