match until word not found in group?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Jipd
Forum Newbie
Posts: 1
Joined: Wed Apr 23, 2008 8:06 pm

match until word not found in group?

Post by Jipd »

Hi

I would like to match a string up until a particular word. The word could be one of several possibilities and may not be present at all.

eg:
thiswordMATCH1
randomMATCH2string
somethingelseNOMATCH
anotherMATCH3possibility

where I would be searching for MATCH1 or MATCH2 or MATCH3 but still match when no word is found.

sentence:(.*)(?!MATCH1|MATCH2|MATCH3)?

I know how to perform negative character matching but not sure about negative word matching, can someone help please?
aCa
Forum Newbie
Posts: 11
Joined: Sun Apr 06, 2008 3:43 pm

Re: match until word not found in group?

Post by aCa »

Would this pattern solve your poblem?

Code: Select all

(.+)(?=MATCH1|MATCH2|MATCH3)
Then I get this result on my tool

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => thisword
            [1] => random
            [2] => another
        )
 
    [1] => Array
        (
            [0] => thisword
            [1] => random
            [2] => another
        )
 
)
 
Is this the result you are looking for?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: match until word not found in group?

Post by prometheuzz »

Jipd wrote:Hi

I would like to match a string up until a particular word. The word could be one of several possibilities and may not be present at all.

eg:
thiswordMATCH1
randomMATCH2string
somethingelseNOMATCH
anotherMATCH3possibility

where I would be searching for MATCH1 or MATCH2 or MATCH3 but still match when no word is found.

sentence:(.*)(?!MATCH1|MATCH2|MATCH3)?

I know how to perform negative character matching but not sure about negative word matching, can someone help please?
Be careful with those greedy (.*) or (.+), they're dangerous! ; )
You're talking about "words" you want to match, but in your example I cannot see how you make a distinction: I don't see any word boundaries (other than the MATCH boundaries). Perhaps there are no boundaries, but maybe there are and you merely posted an example that is not a good representation of the real data you're working with. Also, are you working with "lines" where you want to extract just one (or none at all) word from, or are you working with larger chunks of text and you want to extract multiple matches from it?

Perhaps a bit more detail might result in a more satisfactory answer. And perhaps I am just babbling to myself because the answer of the previous poster already solved your problem, who knows.

Good luck!
Post Reply