[英]Regex for line without runs of non-alphanumeric characters
I want to regex to match eg the below string:我想正则表达式匹配例如下面的字符串:
Discover - CashBack Bonus & Citi TYP®
发现 - 现金返还奖金和 Citi TYP®
All of the non-alphanumeric characters like -
, &
and ®
, cannot appear one after another.所有非字母数字字符,如
-
、 &
和®
,都不能一个接一个出现。
Tried this: ^[A-Za-z0-9 ]+[-]*[A-Za-z0-9 ]+[&]*[A-Za-z0-9 ]+[®]*[A-Za-z0-9 ]+$
试过这个:
^[A-Za-z0-9 ]+[-]*[A-Za-z0-9 ]+[&]*[A-Za-z0-9 ]+[®]*[A-Za-z0-9 ]+$
Tried this:
^[A-Za-z0-9 ]+[-]*[A-Za-z0-9 ]+[&]*[A-Za-z0-9 ]+[®]*[A-Za-z0-9 ]+$
试过这个:
^[A-Za-z0-9 ]+[-]*[A-Za-z0-9 ]+[&]*[A-Za-z0-9 ]+[®]*[A-Za-z0-9 ]+$
This didn't match because the trailing [A-Za-z0-9 ]+$
requires 1 character of [A-Za-z0-9 ]
at the end of the line, while your string ends with ®
.这不匹配,因为尾随
[A-Za-z0-9 ]+$
在行尾需要 1 个字符[A-Za-z0-9 ]
,而您的字符串以®
结尾。
To match a line without runs of non-alphanumeric characters except space, you can use the regex:要匹配除空格外没有非字母数字字符的行,您可以使用正则表达式:
^.?([A-Za-z0-9 ]++.?)*$
This is essentially这本质上是
[A-Za-z0-9 ]++
- a run of alphanumeric characters or space, followed by [A-Za-z0-9 ]++
- [A-Za-z0-9 ]++
字母数字字符或空格,后跟.?
- at most one extra character, (…)*
- the above occuring any number of times. (…)*
- 以上出现任意次数。 The leading ^.?
领先的
^.?
is there to allow for one extra character at the start of the string.是否允许在字符串的开头多出一个字符。 And it's important to use the possessive quantifier
++
in order to avoid catastrophic backtracking .并且重要的是使用所有格量词
++
以避免灾难性的回溯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.