PDA

View Full Version : Simple Regex help




SilentPanda
Mar 3, 2004, 08:35 AM
So this is the Apple/Tech discussion forum so I hope this is okay here... it's more tech than Apple.

Anyhow... I'm trying to get a regular expression to work. The expression is:

^[^ES]


This should match anything whose first character does not begin with E or S. I know the only data coming in will be E, S, M, and R1. In Perl this works just fine. But when I use the ".matches" method of String in Java it doesn't match R1!

Granted I could do it with a simple if statement but I don't see why it won't work. Anybody? :confused:



jeremy.king
Mar 3, 2004, 09:03 AM
That expression would only match one character, which is why R1 wouldn't work.

You could try something like

^[^ES]\w*

or if R1 is your only two character string, use
^[^ES]|R1


Of course, I have no way to test this at the moment, so let me know if it works.

SilentPanda
Mar 3, 2004, 09:23 AM
I'll give it a shot. I'm installing the 1.4.2 Java SDK right now in case there were some changes between 1.4.1 and 1.4.2 to what I'm using (doubtful but I've been meaning to upgrade anyhow).

The odd thing is that my regex worked just fine in Perl. But I'll try your's as soon as the install is done.

Thanks!

SilentPanda
Mar 3, 2004, 09:30 AM
^[^ES]\w*


This one worked like a champ. I also tried
^[^ES]*?


which worked too. Yeah! Still weird that it worked in Perl but not in Java... oh well... different engines I guess... thanks for the help!

:D