regular expression to match english words with som

2020-07-15 15:10发布

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

标签: regex
3条回答
不美不萌又怎样
2楼-- · 2020-07-15 15:49

You are looking for this pattern.

^[\s\w\d\?><;,\{\}\[\]\-_\+=!@\#\$%^&\*\|\']*$
查看更多
【Aperson】
3楼-- · 2020-07-15 15:49

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

查看更多
一纸荒年 Trace。
4楼-- · 2020-07-15 15:56

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.

查看更多
登录 后发表回答