Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Something simple but beyond me

Last post 01-08-2009, 8:34 AM by prometheuzz. 5 replies.
Sort Posts: Previous Next
  •  01-08-2009, 5:32 AM 49948

    Something simple but beyond me

    Hi,

    I've just started using java 2 reg exp in a testing tool. I've a simple match to do but it's beyond me....

    Data - 100 items:
    general0001
    general0002
    general0003
    ...
    general0098
    general0099
    general0100

    I want to match the range general0001 to general0030 which I can do with the expression:
    general00([0-2][0-9]|30)

    I also want to match the same range but exclude general0020....I think I need to use ^ and a back reference... but am totally lost.

    Any examples I've tried looking at use so many other control characters that I get lost in how it works.

    Any help appreciated.

    Cheers

    efj.

     

  •  01-08-2009, 5:41 AM 49950 in reply to 49948

    Re: Something simple but beyond me

    ^general00(?!20)(?:[0-2]\d|30)$
  •  01-08-2009, 6:19 AM 49952 in reply to 49950

    Re: Something simple but beyond me

    Grand works a treat, thanks prometheuzz.

    Having spent 20 mins looking up the various special characters you've used some cogs have started to turn in a little used part of the brain... so I've tried to explain in my own words what is happening. If you're also able comment on my thoughts thanks again.

    1)   ^general00       $
     -show the start and end of the word to match (add this as best practice/effeciency?) prefix of general00

    2)   (?!20)
    -match where general is not followed by 20 i.e don't match 20.

    3)   (?:    )
    -create a non capturing group (add this as best practice/effeciency rather than a capturing group)

    4)   [0-2]\d
    -match on 0 or 2 followed by any single digit

    5)   |30
    -or the value 30.

    Cheers.
     

  •  01-08-2009, 6:39 AM 49953 in reply to 49952

    Re: Something simple but beyond me

    efj:

    Grand works a treat, thanks prometheuzz.

    ...

    Good to hear that!

    efj:

    1)   ^general00       $
     -show the start and end of the word to match (add this as best practice/effeciency?) prefix of general00

    Not the start and end of a word: but rather the start and end of the input string (although that is what you meant, is my guess). I added those meta characters (^ and $) because in many regex-flavors, if a part of the input string matches, the value 'true' (or something similar) is returned. That would mean that the string "general002500", or "ABCgeneral0025" would also match since the underlined part matches. So, that's why I anchored the start and end of the string.

    efj:

    2)   (?!20)
    -match where general is not followed by 20 i.e don't match 20.

    Where "general00" is not followed by "20", although, again, I suspect a typo on your side and that you really meant to type "general00". So, yes, that's true.

    efj:

    3)   (?:    )
    -create a non capturing group (add this as best practice/effeciency rather than a capturing group)


    True. Just a habit on my side: note that the "readability" of a regex is also important. So, if you're not comfortable with them (not true in your case, I see), or are working in a group of developers, you can of course leave them out. Especially when the regex-patterns and input strings are small (then little difference can be made in that case).

    efj:

    4)   [0-2]\d
    -match on 0 or 2 followed by any single digit

    5)   |30
    -or the value 30.

    Yes and yes.

    efj:

    Cheers.

    No problem, glad to be of help. It's always nice to be able to help someone who is able to put his/her problem into a meaningful, coherent question and tries to figure things out by themselves instead of posting an immediate follow up question!

    Kind regards,

    Bart.

  •  01-08-2009, 8:13 AM 49955 in reply to 49953

    Re: Something simple but beyond me

    Excellent, it's all making sense including your comments above. You were right re my typos etc. No doubt my follow up question will be posted in about an hour!

    Cheers.

  •  01-08-2009, 8:34 AM 49957 in reply to 49955

    Re: Something simple but beyond me

    efj:

    Excellent, it's all making sense including your comments above. You were right re my typos etc. No doubt my follow up question will be posted in about an hour!

    Cheers.

    No problem, if your future questions are of the same quality as your first one, I'm more than willing to give you a helping hand.

View as RSS news feed in XML