Hello World
Hello World

Reputation: 1437

regular expression to match two words with a space

I am looking for the regular expression needed to match the follow pattern

hello, hello world, helloworld

I would only want the bold set to be picked up (the space included.)

Upvotes: 10

Views: 26819

Answers (3)

林芳玉
林芳玉

Reputation: 21

^hello\sworld$
  • ^ will anchor the beginning of the string, the $ will anchor the end of the string
  • hello: matches the first word 'hello'
  • \s: matches the space
  • world: matches the second word 'world'

Upvotes: 2

polkduran
polkduran

Reputation: 2551

\b[a-zA-Z]+\s[a-zA-Z]+\b

as \w could match numbers also.

\b is a word boundary

Upvotes: 0

tsm
tsm

Reputation: 3658

\w+\s\w+

At least one word character, then a space (or tab, or what have you), then at least another word character.

Here's what it looks like:

enter image description here

Upvotes: 13

Related Questions