#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    9
    Rep Power
    0

    Question Regex that excludes specified substring


    I need to write a regex validation that would pass text that DOESN'T contain a specified substring. For example, given five strings:

    Arrowhead
    Arrival
    Parrot
    Rowland
    Melrow

    This regex should pass all of them who don't contain ROW. So the proposed filtered output would be :

    Arrival
    Parrot


    How can I do this in the most elegant way?

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    It's far easier to search for the substring and place a not in your if statement. (and since your substring is constant using whatever string primitives your language provides should be a better solution than regex)

    If you really must do it the confusing way, /^(?:(?!substring).)*$/.

    Comments on this post

    • requinix agrees
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. Autodidact
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Location
    Canada
    Posts
    741
    Rep Power
    833
    I think lookaheads are more than what Gatwick requires.

    I'd wrap this in a function that replaces the substring, and returns true or false.

    Code:
    /^(.*)?substring(.*)?$/
    Soldiers, from the height of these pyramids forty centuries look down upon you.
    ▪ Napoleon, on the eve of the Battle of the Pyramids

IMN logo majestic logo threadwatch logo seochat tools logo