Page 1 of 1
match until word not found in group?
Posted: Wed Apr 23, 2008 8:21 pm
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?
Re: match until word not found in group?
Posted: Thu Apr 24, 2008 3:22 am
by aCa
Would this pattern solve your poblem?
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?
Re: match until word not found in group?
Posted: Fri Apr 25, 2008 3:52 am
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!