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

    Join Date
    Oct 2004
    Location
    london
    Posts
    469
    Rep Power
    84

    Using Regex to match a whole string starting and endign with a word


    I am trying to match a whole string using Regular Expressions. Despite reading lots of posts I can't work out just how to do it.

    For example, in
    Code:
    The quick brown fox jumped over the lazy dog
    I want to select everything between brown and lazy, i.e. I want to get

    Code:
    brown fox jumped over the lazy
    I am using regex of:
    Code:
    (brown).*(lazy)
    Is that right? Is there a more efficient way?

    Thanks in advance for any help. I am a regex newbie and pulling my hair out!
    Interested in online gaming? Access full reviews of all UK Online casinos here.

    Roulette Strategy |William Hill | Learn about Wine | Jackpot joy
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Normal string functions are generally better for this: find the offset of "brown", find the offset of "lazy" after it, and grab everything in between them.

    But in terms of regular expressions, yeah that's basically it. You might want the parentheses in a different spot and maybe a space between the words:
    Code:
    brown (.*) lazy

    Comments on this post

    • dominoman agrees : Thankyou!
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Without disagreeing with requinix, I'd also suggest you consider a small tweak:

    brown.*?lazy
    The question mark after the star ensures that you only match characters between brown and the first lazy (if you had several), as in:
    That brown snake is a lazy snake, if I ever saw a lazy snake.
    Without the question mark, the regex matches:
    That brown snake is a lazy snake, if I ever saw a lazy snake.

    Comments on this post

    • requinix agrees : yeah, might as well
    • dominoman agrees : Great. I'll get the hang of this regex soon
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    london
    Posts
    469
    Rep Power
    84
    Originally Posted by ragax
    Without disagreeing with requinix, I'd also suggest you consider a small tweak:

    brown.*?lazy
    The question mark after the star ensures that you only match characters between brown and the first lazy (if you had several), as in:
    That brown snake is a lazy snake, if I ever saw a lazy snake.
    Without the question mark, the regex matches:
    That brown snake is a lazy snake, if I ever saw a lazy snake.
    Many thanks for the advice. I tested those and they work perfectly in the online regex tester, they don't work for WP-o-matic. It matches the whole article

    WP-o-matic is a great tool but it doesnt do what it should with regular expressions.
    Interested in online gaming? Access full reviews of all UK Online casinos here.

    Roulette Strategy |William Hill | Learn about Wine | Jackpot joy

IMN logo majestic logo threadwatch logo seochat tools logo