Reputation: 3523
I would like to use regular express to check lines that contains only one word
Say:
'hello'
'hello World'
I tried \w+
, but it returns 'hello world' too, any suggestions?
Thanks,
Upvotes: 2
Views: 3768
Reputation: 353
The accepted answer is correct. However, if someone wants to allow special characters in the word too, then ^(\S+)$
would work.
Upvotes: 0
Reputation: 2347
You need to add anchors for beginning and end of string.
Try this:
^(\w+)$
You can try a live demo here. Notice the multiline parameter, depending on your language you either append /m
to the regex or pass this as a parameter.
It will match lines with only one word. The other way to do this is to add "lookbehind" and "lookahead", however, this way is easier.
Upvotes: 4