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

Match text between two words

  •  12-10-2007, 5:51 PM

    Match text between two words

    Let this text:

        WORD1 blahblah1 WORD1 blahblah2 WORD2

    (Note that WORD1 is repeated 2 times, could be more, and that "blahblah" is some random text, always changing, but matched with .*\s*)

    The problem is: You want to match when WORD1 is followed at any distance by WORD2, but without any other WORD1 in the way. I mean:

        "WORD1 blahblah2 WORD2" --> match

        "WORD1 blahblah1 WORD1 blahblah2 WORD2" --> not match, although starting also with WORD1 and ending with WORD2,

    but it has another WORD1 in the middle.

    I tried this pattern (WORD1.*\s*){1}WORD2 as a way to avoid "WORD1 blahblah" repetitions... but doesn't work! Don't know why...

    Thanks in advance. 

    P.D.: I'll use TRegExpr library 

    Filed under: ,
View Complete Thread