Reputation: 2673
I'm trying to match (look for) two words in a string which is as follows:
Mac OS X/10.11.5 (15F34); ExchangeWebServices/6.0 (243);
I want to match (true) if we see "Mac
" AND "ExchangeWebServices
" but the characters between the two words would be unknown/random. Can someone help me with the regex syntax?
Thanks!
Upvotes: 7
Views: 58842
Reputation: 1
(apple|banana)
will ALSO match "applebanana"... try this instead:
(\bapple\b|\bbanana\b)
That will match either the whole word apple or the whole word banana, but not applebanana or bananaapple
Upvotes: 0
Reputation: 125
With this regular epression you can find the words and anything in between even if the words are longer than the words in the regular expression:
(Mac).*?(ExchangeWebServices).*?
It would find this string:
Mac OS X/10.11.5 whatever text in between (15F34); ExchangeWebServices/6.0 (243);
So, if you write the words without the end (ExchangeWebServ):
(Mac).*?(ExchangeWebServ).*?
It would find both strings:
Mac OS X/10.11.5 whatever text in between (15F34); ExchangeWebServices/6.0 (243);
And:
Mac OS X/10.11.5 whatever text in between (15F34); ExchangeWebServ/6.0 (243);
If you need to match exact words you'll need to use \b
as mentioned in the answers above.
Upvotes: 3
Reputation: 3619
This will match exactly the words "Mac" and "ExchangeWebServices" with anything else between them:
\bMac\b.*\bExchangeWebServices\b
Regex 101 Example: https://regex101.com/r/sK2qG1/4
Upvotes: 18
Reputation: 375
This is a simple regular expression can be get by
/^Mac.+ExchangeWebServices/
We are assuming 'Mac' and 'ExchangeWebServices' are two different words separated by some character.
you can follow the link to learn more of regular expressions Learning Regular Expressions
Upvotes: 5