Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to match english words with some other characters

Tags:

regex

I use this regular expression: ^[a-zA-Z0-9]*$ to match English phrases, however, I want this expression to match English phrases that may contain some or all of these characters at the beginning, between or at the end of them: ? > < ; , { } [ ] - _ + = ! @ # $ % ^ & * | ' and also the space character.

how can I update this regular expression to satisfy this requirement ?

thank you so much in advance ...

like image 503
JAHelia Avatar asked Jan 22 '12 14:01

JAHelia


2 Answers

You could simply add all your desired characters to your character class.

^[a-zA-Z0-9?><;,{}[\]\-_+=!@#$%\^&*|']*$

You will need to escape the following characters with a backslash, since they are considered as metacharacters inside character classes: ], -, ^.

Note that your regex will also match empty strings, since it uses the * quantifier. If you only want to match words having at least one character, replace it with the + quantifier.

like image 92
Douglas Avatar answered Oct 04 '22 15:10

Douglas


You are looking for this pattern.

^[\s\w\d\?><;,\{\}\[\]\-_\+=!@\#\$%^&\*\|\']*$
like image 23
Shiplu Mokaddim Avatar answered Oct 04 '22 16:10

Shiplu Mokaddim