Simple Regex help

Discussion in 'General Mac Discussion' started by SilentPanda, Mar 3, 2004.

  1. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #1
    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:
    Code:
    ^[^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:
     
  2. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #2
    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.
     
  3. SilentPanda thread starter Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #3
    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!
     
  4. SilentPanda thread starter Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #4
    Code:
    ^[^ES]\w*
    
    This one worked like a champ. I also tried
    Code:
    ^[^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
     

Share This Page