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

regex that will negate a word

Last post 11-20-2009, 1:13 PM by mash. 7 replies.
Sort Posts: Previous Next
  •  11-19-2009, 12:17 PM 57415

    regex that will negate a word

    Hello, I have a problem with constructing perl regular expression that will negate a word.

    For example, I want to match every string that is alpha-numerical BUT does NOT BEGIN with the word "blog".

    blog -> not matches

    blogsomething -> not matches

    somethingblog -> matches

    Thanks in advance.

  •  11-19-2009, 12:44 PM 57416 in reply to 57415

    Re: regex that will negate a word

    use a negative lookbehind:

     

    ^(?!=:blog) or (?<!blog)([a-z0-9]+)

  •  11-19-2009, 5:23 PM 57418 in reply to 57416

    Re: regex that will negate a word

    Jewbacca, the problem with using a negative lookbehind as in your suggestions (apart from the syntax in your first one - that is a negative lookahead that uses the string that starts with an equals sign and colon) is that, on its own, it doesn't solve the problem.

    Take the word "blogshpere". When the pattern starts on that word, there is nothing to look behind at and so the rest of the pattern matches quite happily.

    What you need to do is to anchor the pattern to force it to start at the beginning of the word. Once you have that anchor, you can then use a negative lookahead to check for the "blog" character string and go from there. My suggestion would be:

    \b(?!blog)[a-z0-9]+

    of course using the 'ignore case' and whatever other options and adding matching groups etc as needed.

    Susan

     

  •  11-20-2009, 7:54 AM 57421 in reply to 57418

    Re: regex that will negate a word

    Nope both are for lookbehind according to the bible ;)

     

    Actually I was bass ackwards yesterday as my lookbehind should have been at the end, ironically it doesn't work but it does work when it's not negated (weird) to match words beginning with blog. If you think about it, it should work.

     

    Fortunately regex is like perl, more than one way of doing it .

  •  11-20-2009, 8:33 AM 57422 in reply to 57421

    Re: regex that will negate a word

    Thank you very much, I solved the problem with your solutions.
  •  11-20-2009, 10:49 AM 57423 in reply to 57421

    Re: regex that will negate a word

    JewBacca:

    Nope both are for lookbehind according to the bible ;)

    No Susan is correct, so your source is mistaken.

    (?!=) is a negative lookahead. It processes forward in the string.

    Both syntaxes you used are look-arounds but they perform different actions


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-20-2009, 11:03 AM 57424 in reply to 57423

    Re: regex that will negate a word

    I don't think it's either as I tried to use it(haven't used it before) as both (tried (?! first) or it could be language dependent
  •  11-20-2009, 1:13 PM 57425 in reply to 57424

    Re: regex that will negate a word

    JewBacca:
    I don't think it's either as I tried to use it(haven't used it before) as both (tried (?! first) or it could be language dependent

    Lookarounds support is implementation dependent.   Look-aheads are more widely supported in programming languages.  If Lookarounds are supported at all then look-aheads are supported.  Look-behinds are only supported by a few langauges/platforms, Perl, PHP and .Net being the ones that leap to mind, and the implementation  is not identical.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
View as RSS news feed in XML