I have a working regex in Python and I am trying to convert to Java. It seems that there is a subtle difference in the implementations.
The RegEx is trying to match another reg ex. The RegEx in question is:
/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)
One of the strings that it is having problems on is: /\s+/;
The reg ex is not supposed to be matching the ending ;
. In Python the RegEx works correctly (and does not match the ending ;
, but in Java it does include the ;
.
The Question(s):
Java doesn't parse Regular Expressions in the same way as Python for a small set of cases. In this particular case the nested [
's were causing problems. In Python you don't need to escape any nested [
but you do need to do that in Java.
The original RegEx (for Python):
/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)
The fixed RegEx (for Java and Python):
/(\\.|[^\[/\\\n]|\[(\\.|[^\]\\\n])*\])+/([gim]+\b|\B)
The obvious difference b/w Java and Python is that in Java you need to escape a lot of characters.
Moreover, you are probably running into a mismatch between the matching methods, not a difference in the actual regex notation:
Given the Java
String regex, input; // initialized to something
Matcher matcher = Pattern.compile( regex ).matcher( input );
matcher.matches()
(also Pattern.matches( regex, input )
) matches the entire string. It has no direct equivalent in Python. The same result can be achieved by using re.match( regex, input )
with a regex
that ends with $
.matcher.find()
and Python's re.search( regex, input )
match any part of the string.matcher.lookingAt()
and Python's re.match( regex, input )
match the beginning of the string.For more details also read Java's documentation of Matcher
and compare to the Python documentation.
Since you said that isn't the problem, I decided to do a test: http://ideone.com/6w61T
It looks like java is doing exactly what you need it to (group 0, the entire match, doesn't contain the ;
). Your problem is elsewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With