how to negate a regexp match

Hey,

I’m trying to write a trigger action property expression that will fire when a regexp does not match…

Perl syntax is ‘string’ !~ regexp, but that doesn’t seem to work.

I read a post by Tim in the forum suggesting !~= but that has the =~ in the wrong direction, so I tried !=~, but that didn’t seem to work either.

Can someone straighten me out? Thanks, jas…

So I got it working. Again, it was this stupid problem of needing to have .* in front of and behind the regexp. So this doesn’t work:

SplopLocations!~$BasicName$

but this does:

SplopLocations!~.*$BasicName$.*

If this is a feature, I don’t enjoy it…

The usage of .* has never been a required feature for Vassal property
syntax, unless you are using the 3.2 trunk and it has stuff incorporated
that might require syntax change from what is used now (Cant say as I do not
know how that is going to work - haven’t messed with it)

Post generated using Mail2Forum (mail2forum.com)

Thus spake “jestew”:

Wow, that’s positively awful, if you’re in fact using it correctly,
and we need to change that.


J.


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

Post generated using Mail2Forum (mail2forum.com)

I’m working on 3.1.4…

Here is my other post in the Tech Support forum:

vassalengine.org/forums/viewtopic.php?t=2005

Cheers, jas…

Well if it is a feature old or new - its never been publicized, otherwise
I’d say you’ve found something screwy going on :slight_smile:

Post generated using Mail2Forum (mail2forum.com)

I will look into this, I know where the bodies are buried. It should not be required.

B.


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

Post generated using Mail2Forum (mail2forum.com)

This is working exactly as I would expect.

The right hand side of !~ is a regular expression. Does $BasicName$ evaluate to a regular expression? Highly unlikely unless you have very weirdly named counters. Hence the need for you to the ‘.*’ to force $BasicName$ to be a regular expression that matches your locations.

I suspect that what you actually want is

BasicName!=$SplopLocations$

B.


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

Post generated using Mail2Forum (mail2forum.com)

Then we expect different things ;-)

Perhaps this is the problem - what do you mean does it evaluate to a regular expression? The documentation is very sparse on this, and I have only been able to find 3 or 4 posts discussing the use of regexes in the forums. I have a lot of experience using regexes with Perl and grep, but it is always a question of syntax.

Here is the behavior I expext:

DB<4> print “true” if “GlutEyeForebostinator” !~ /Head/
==>true
DB<5> print “true” if “GlutEyeForebostinator” !~ /Glut/
==>false
DB<6> print “true” if “GlutEyeForebostinator” !~ /Eye/
==>false

SplopLocations is a concatenation of 3 counter names - I simply want to match every counter name against the concatenation to see if it doesn’t match…

Is my problem a syntax problem?

Am I not understanding some fundamental difference with Java regexes? From reading the JDK javadocs, they seem 100% compatible when programming in Java - but this isn’t Java, this is VASSAL…

Once I figure this out, I’m happy to contribute a documentation patch.

Cheers, jas…

*********** REPLY SEPARATOR ***********

On 10/05/2009 at 12:06 AM jestew wrote:

I no nothing about Perl, but /Head/ and /Glut/ are not regular expressions as such and will not match anything but themselves. The Perl !~ operator must be implying some additional behaviour.

If SplopLocations was set to ‘Glut|Eye|Forebostinator’, then

BasicName!~$SplopLocations$ would do what you want.

B.


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

Post generated using Mail2Forum (mail2forum.com)

Hey Brent,

Thanks for taking the time to look into this and help make it more clear.

This is not true to my experience. Perhaps I’m not clear.

Perl (and grep) use the forward slash to delimit the regex. So /Glut/ is a fine, but simple, regexp.

Any string is a regular expression - and it will match any string that contains it. For example:

“Glut” =~ Glut ==> true

Like you said - not so useful - I could just use ‘=’ for such a simple case, but this is also true:

“HeadGlutEye” =~ Glut ==> true

And this is what I’m trying to do, or more to the point this:

“HeadGlutEye” !~ Foot ==> true

I want to see that the location Foot is not in the string.

Now this is also true, but it should not be necessary:

“HeadGlutEye” !~.Foot. ==> true

The extra matching all characters at the front and back of the string are totally pointless. Right now, this is required in VASSAL to get things working - so I think something is broken.

None, it is just the analogous operator of != for regexes, just as it seems it is being used in VASSAL - but again there is nothing about it in the documentation - so I could be horribly wrong - I’m using it just like the perl operator.

Yes, that would work, too - but trying to use VASSAL to build up the string ‘Glut|Eye|Forebostinator’ is much harder than simply concatenating them all together and doing the opposite match. In Java, I’d just have a list, and then join each element with a ‘|’… nice and dandy… I haven’t worked out how to do that with VASSAL yet.

More to the point, what I’m trying should work. Again, unless I am fundamentally missing something about how VASSAL is implementing regexes. So I hope to figure out either what hole there is in my understanding, or help identify a potential bug.

Thanks again for your time Brent. jas…

Hi Jas,

I know very little about regexps and how they ‘normally’ work, Joel is much better placed to comment, but I suspect I know the problem.

The regexp matcher is using

return Pattern.matches(regexp, string);

to check the pattern match, when I suspect it should be

return Pattern.compile(regexp).matcher(string).find();

The first version appears to try and match the entire string against the regexp, while the second I think does what you want.

Reference:
java.sun.com/j2se/1.5.0/docs/api … ttern.html

B.

*********** REPLY SEPARATOR ***********

On 10/05/2009 at 1:29 AM jestew wrote:


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

Post generated using Mail2Forum (mail2forum.com)

Hey Brent,

Haha - we posted the same answer at the same time in two different threads… Same same but different :slight_smile:

Cheers, jas.

Thus spake “Brent Easton”:

That is completely other than how I would expect this to work.

One problem is that we’re missing some necessary delimiters around the
pattern:

SplopLocations !~ /$BasicName$/

Those slashses need to be there, otherwise there’s no way to tell the
difference between $BasicName$ and $BasicName$ with whitespace on either
side of it.

The primary problem I see, however, is that $BasicName$ and .$BasicName$.
as a one-off match pattern should be equivalent. If $BasicName$ contains the
string ‘foo’, then both should match exactly the strings which contain ‘foo’
as a substring. If they don’t, that’s broken.


J.


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

Post generated using Mail2Forum (mail2forum.com)

Yes, I think that would clear up potential ambiguity, and it would make it identical to grep and sed and Perl, etc…

And as Brent pointed out, using Matcher.find() would fix the second issue. Cheers, jas…

Thus spake “Brent Easton”:

Yes, and we should fix that. The slashes are pattern delimiters. Without
them, you can’t write patterns like / foo /.


J.


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

Post generated using Mail2Forum (mail2forum.com)

In the longer term, the current ‘Property Expressions’ need to be replaced by a BeanShell implementation, where Regexp’s will be handled by a function. You’ll do something like

matches(BasicName, “/foo/”);

B.


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

Post generated using Mail2Forum (mail2forum.com)

This is what really confused me. You are using the string to search for as the regexp and the string to search in as a standard string.

I would have done it this way:

Glut=~Head|Glut|Eye
Foot!~Head|Glut|Eye

Does the String ‘Glue’ match/‘Foot’ not match the Regular expression ‘Head|Glut|Eye’


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

Post generated using Mail2Forum (mail2forum.com)

My use of regexes comes from the perspective of a Unix sysadmin - you are always asking the find() question… I know a word or a phrase, and I want to know all lines that contain that word or phrase someplace…

This Pattern.matches() concept of the JDK is a new one for me.

Yes - that would work. I chose not to do it that way because I need to construct the string at the beginning of the game when the player hides three pieces in different locations. It was much simpler with VASSAL to just concatenate all the locations together into a single string - without the ‘|’ separator. I’m still learning the tricks of VASSAL…

I would like to store the names in a list and when the player makes a check I’d search each member of the list… But since I don’t have lists - I figured I’d use a string and do a regex search.

I know how to get the string: |Head|Glut|Eye - but I havent figured out how to remove that first ‘|’ character…

Sorry, I’m not sure what you’re asking - could you say it differently?

Cheers, jas…


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

Post generated using Mail2Forum (mail2forum.com)
[/quote]

I’ve only ever used Regexps from the other perspective - is the String S described by the regexp R or not? This is really the sense in which the regexps are used in Property Match Expressions. You and Joel are a special subclass of users that leads you to expect certain behaviour from the ‘=~’ and ‘!~’ because they happen to look the same as the Perl operators. The Perl form of behaviour is not necessarily what we need. However, my past experience of trying to change the minds of *nix people leads me to pull completely out of this conversation as this point and leave it to Joel to sort out :slight_smile: I am not sure what effect the changes might have on existing modules.

Your scheme will fall apart if the search expressions is Foot and the options included FootSore and Footloose, it is not a general purpose solution.

Don’t worry, i’ts just an example.


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

Post generated using Mail2Forum (mail2forum.com)