4

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!

Jason Shave
  • 1,693
  • 2
  • 16
  • 38

3 Answers3

15

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

Jim
  • 2,273
  • 1
  • 12
  • 21
2

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

Community
  • 1
  • 1
krishnakant
  • 305
  • 1
  • 3
  • 16
0

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.

mrvinent
  • 95
  • 16