简体   繁体   English

正则表达式将单词与数字匹配,但如果仅数字则不匹配

[英]Regex to match a word with numbers but not if it's numbers only

I would like to match a word that may contain numbers but not if it's numbers only. 我想匹配一个可能包含数字但不包含数字的单词。

For example: 例如:

Menial = match Menial = 比赛

John124 = match John124 = 比赛

V4n3ss4 = match V4n3ss4 = 匹配

0livia = match 0livia = 匹配

3715 = no match 3715 = 不匹配

I have tried \\w* but surprisingly, this takes a plain number as a word. 我已经尝试过\\w*但是令人惊讶的是,这需要一个普通数字作为单词。 I also tried other patterns with \\b but I noticed that, for some reason, a single number is also considered a "word boundary". 我也用\\b尝试了其他模式,但是我注意到,由于某种原因,单个数字也被视为“单词边界”。

One approach to force a letter somewhere in the middle of the match is to put [A-Za-z] in between \\w* expressions, like this: 在比赛中间某个位置强制输入字母的一种方法是将[A-Za-z]放在\\w*表达式之间,如下所示:

\b\w*[A-Za-z]\w*\b

\\b s on both ends ensure that the matches are at word boundaries. 两端的\\b s确保匹配项位于单词边界。

Demo. 演示。

Try: \\w*[a-zA-Z]\\w* 试试: \\w*[a-zA-Z]\\w*

This is essentially "At least one non-digit character surrounded by any number of alphanumeric characters" 本质上是“至少一个由任意数量的字母数字字符包围的非数字字符”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM