Thread: Regex Help

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

    Join Date
    Jun 2011
    Posts
    57
    Rep Power
    4

    Regex Help


    I need help with searching for a specific word up to a certain point in a sentence.

    For instance lets say my sentence is:

    This is a sentence that I want to use for a example:

    If I want a regex that will exit if it finds any 'a' before the word sentence. Is it this easy:

    Code:
    while ($word =~ /a.+sentence/mxsi){
    exit;
    }
    else{
    return;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,955
    Rep Power
    1225
    I'd need more context to say if that regex is the best choice, but it should be fine. The main issue is that you should be using an if block instead of a while block.

    Code:
    if ($word =~ /a.+sentence/mxsi) {
        exit;
    }
    else {
        return;
    }
    Which could be written as:
    Code:
    $word =~ /a.+sentence/mxsi ? exit : return;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    57
    Rep Power
    4
    Thanks, will try.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    This regex should work, but you will have to think about what you want to do if the word "sentence" comes twice in your sentence.

IMN logo majestic logo threadwatch logo seochat tools logo