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

Regex to exclude another pattern

Last post 04-04-2008, 7:27 AM by coolrb. 5 replies.
Sort Posts: Previous Next
  •  04-02-2008, 11:43 AM 40965

    Regex to exclude another pattern

    Hi, I am developing a regular expression that will test whether a string ends with test1234 or man2345.

    E.g.  coolguy/most/test1234 is invalid where as coolguy/most/testtest is valid. The pattern I have developed is coolguy/most/.*(?<!(test[0-9]{4}|man[0-9]{4})). But I am getting error "Look-behind group does not have an obvious maximum length near index 54...".

     

    I would appreciate your help on this.

     

    - BR 

    Filed under:
  •  04-02-2008, 1:04 PM 40971 in reply to 40965

    Re: Regex to exclude another pattern

    how do u plan to validate your strings:

    1. one-by-one as a separate strings/lines

    2. as parts of a text

    ?

  •  04-02-2008, 6:37 PM 40995 in reply to 40965

    Re: Regex to exclude another pattern

    You don't mention the language or regex engine that you are using, but going by the error message, some regex engines only allow fixed length look-behinds. Now each of the alternate paths you have are fixed length, but together they could be 7 or 8 characters long. this is a possible cause for your error.

    If you have a regex engine that can handle lookbehind, you will also be able to use lookahead. Most regex's that allow lookahead also allow variable length lookahead patterns, so perhaps something like (untested):

    coolguy/most/(?!.*?(test|man)\d{4}).*

    may work.

    Susan 

  •  04-03-2008, 1:05 AM 41001 in reply to 40971

    Re: Regex to exclude another pattern

    I just wanted to validate one by one string. I use java for regex.

    I tried with your suggested pattern but it seems do not work.

  •  04-03-2008, 6:11 PM 41046 in reply to 41001

    Re: Regex to exclude another pattern

    In what way didn't it work? Can you post the code you are using together with your test data and the expected results?

    Susan 

  •  04-04-2008, 7:27 AM 41069 in reply to 41046

    Re: Regex to exclude another pattern

    It works, earlier it was my mistake to apply it properly.

    I use it in the following way

    public boolean testPattern(String url){
            String regex = "coolguy/most/(?!.*?(test|man)\\d{4}).*";
            return Pattern.matches(regex, url));
        }

View as RSS news feed in XML