Skip to content Skip to sidebar Skip to footer

Regex For Phrase That Has Special Chars

I want to make regex that will find phrases between 1 and 4 words. First letter of first word must be capital, other can be lower. These words can contain special chars in them or

Solution 1:

try this regex : \d{0,3}[A-Z][A-Za-z.\-&$']*( +[A-Za-z.\-&$']+){0,3}(?:\n|$)

First, \d{0,3} check if the phrase begin with 0 to 3 number.

Next, [A-Z][A-Za-z.\-&$']* match the first word that need a capital letter first.

Next, ( +[A-Za-z.\-&$']+){0,3} match a space and then a series of character to form a word with a maximum of 3 occurance (4 words total).

Finally, (?:\n|$) match a newline or the end of the string without incuding it in the result.

if you want different special character just add them inside the two [] like this : [A-Za-z"special character you want here without quotation mark"]

Note that '-' character need escape so write '\-' instead.

check on this site to confirm : regexr.com/61f8c

Post a Comment for "Regex For Phrase That Has Special Chars"