How to find/match whole words where there may be one or more dashes but only inside the word - not on the boundaries?
Correct:
w
wo
w-o
wo-r-d
Not correct/rejected:
-
-w
w-
-word
wo-rd-
I've tried several patterns on https://regex101.com but they all fail
CodePudding user response:
Given only dashes are allowed characters within strings, you can use the following regex:
^[A-Za-z](?:[A-Za-z\-]*[A-Za-z])?(?=\s|$)
Regex Explanation:
^
: start of string symbol[A-Za-z]
: a character (enforces word to start with a letter)(?:[A-Za-z\-]*[A-Za-z])?
: optional combination of other characters and dashes, ensuring that last symbol is a character(?=\s|$)
: a space or end of string symbol (ensures your word has ended)
Check the demo here.
CodePudding user response:
You may use the following pattern:
^(\w|\w[\w-]*?\w)$
The above pattern matches:
- a single word character (
\w
), OR - a single word character, followed by as many word or dash characters (the
[\w-]*?
is greedy), until it reaches the final word character
\w
matches any word character (equivalent to [a-zA-Z0-9_]
). If you wish to have only letter, use [a-zA-Z]
instead.