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?
match until word not found in group?
Moderator: General Moderators
Re: match until word not found in group?
Would this pattern solve your poblem?
Then I get this result on my tool
Is this the result you are looking for?
Code: Select all
(.+)(?=MATCH1|MATCH2|MATCH3)Code: Select all
Array
(
[0] => Array
(
[0] => thisword
[1] => random
[2] => another
)
[1] => Array
(
[0] => thisword
[1] => random
[2] => another
)
)
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: match until word not found in group?
Be careful with those greedy (.*) or (.+), they're dangerous! ; )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?
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!