problems with regular expressions

Hi,

If this is a misunderstanding and not a bug, I appologize.

I am attempting to match a Global Property, SplopLocations, using the regular expression of a piece’s $BasicName$ property.

So the property may look like “EyeGlutForebostinator”, and the BasicName is “Glut”. In a trigger action I attempted this:

SplopLocations =~ $BasicName$

but this failed. However if I add ‘.*’ to the front and the back of the match it suddenly works:

SplopLocations =~ .*$BasicName$.*

As another example, if SplopLocations was “Eye”, then the following works:

SplopLocations =~ ^Eye

but if SplopLocations is “EyeGlut”, then the example does not work!! This is completely wrong.

Please help me understand if I am confused. Cheers, jas…

I seem to answer my own replies too often…

Brent has supplied part of the answer here:

vassalengine.org/forums/view … =7771#7771

I removed the spaces after the =~ and before the $BasicName$ but it still doesn’t solve the problem.

Also, the problem exists for regexp negation using !~

Any suggestions what I’m doing wrong? Thanks, jas…

OK, at least I understand that the problem is not a VASSAL problem. It is a bigger issue with how Java appears to implement regexes.

Here is the output of a Perl and Java program running the same regexes against the same strings:

jasons@jackal:~/Documents/VASSAL/code$ java Test
regex Glut doesn't match: EyeGlutForebostinator
regex Glut.* doesn't match: EyeGlutForebostinator
regex .*Glut.* matches: EyeGlutForebostinator

jasons@jackal:~/Documents/VASSAL/code$ perl ./test.pl
Regex Glut matches: EyeGlutForebostinator
Regex Glut.* matches: EyeGlutForebostinator
Regex .*Glut.* matches: EyeGlutForebostinator

Notice they are completely different. Java and Perl do not agree about regular expressions. I found the explanation here

The punch line is:

Looking at src/VASSAL/counters/PropertiesPieceFilter.java

accept() is coded using Pattern.matches() - so this explains what is happening. If instead of using Pattern.matches() if it were to be recoded using Matcher.find() you would get a much more universal behavior that matches the expectations of users familiar with unix (grep) regexes and Perl regexes.

Using Matcher.find() the results are the same as for Perl:

jasons@jackal:~/Documents/VASSAL/code$ java Test2
regex Glut matches: EyeGlutForebostinator
regex Glut.* matches: EyeGlutForebostinator
regex .*Glut.* matches: EyeGlutForebostinator

Any thoughts about this?

Thanks for your time. Cheers, jas…

Exactly what Java code are you using to evaluate this?

To my naive eyes, the Java version is correct and the Perl expression is incorrect.

I believe Java is checking if the string matches the entire regular expression, whereas Perl is returning true if any subset of the string matches the regexp.

B.


Messages mailing list
Messages@forums.vassalengine.org
forums.vassalengine.org/mailman/ … engine.org

Post generated using Mail2Forum (mail2forum.com)

Hey Brent,

Sorry, I went back and edited the post with an explanation. Hope that clears things up.

Now that I understand what is happening, I can code around it. But I believe that using Matcher.find() would be backwards compatible, and it would be much more powerful.

Cheers, jas…

On May 10, 2009, at 2:21 AM, jestew wrote:

This difference depends on whether the regular expression is only
looking for a matching subsequence, or if it needs to match the entire
sequence.

If you use Java, you can control this by choosing find/lookingAt
versus matches as the Matcher method.

I’m not sure how you would accomplish full string matching in Perl.
Perhaps only through the use of ^ and $ elements?


Messages mailing list
Messages@forums.vassalengine.org
forums.vassalengine.org/mailman/ … engine.org

Post generated using Mail2Forum (mail2forum.com)

Yup! That’s what they are there for.

For example if you want to match an empty line:

$string =~ /^$/

Cheers, jas…