JAHelia
JAHelia

Reputation: 7932

regular expression to match english words with some other characters

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 ...

Upvotes: 5

Views: 32047

Answers (3)

Michael G
Michael G

Reputation: 129

I'm thankfully accept the \s\w\d groups from the previous answer, and add other delimiters and special characters as hexadecimal ASCII ranges (you can use Unicode ranges as well):

^[\s\w\d\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]*$

You can refer here to the ASCII Codes and Unicode characters

Upvotes: 3

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

You are looking for this pattern.

^[\s\w\d\?><;,\{\}\[\]\-_\+=!@\#\$%^&\*\|\']*$

Upvotes: 4

Douglas
Douglas

Reputation: 54887

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.

Upvotes: 9

Related Questions