简体   繁体   English

正则表达式:将任何单词与数字匹配

[英]regex: match any word with numbers

I checked on stackoverflow already but didn't find a solution I could use. 我已经检查了stackoverflow,但没有找到我可以使用的解决方案。 I need a regular expression to match any word (by word I mean anything between full spaces) that contains numbers. 我需要一个正则表达式来匹配包含数字的任何单词(通过单词,我的意思是完整空格之间的任何内容)。 It can be alphanumeric AB12354KFJKL , or dates 11/01/2014 , or numbers with hyphens in the middle, 123-489-568 , or just plain normal numbers 123456789 - but it can't match anything without numbers. 它可以是字母数字AB12354KFJKL ,或日期为11/01/2014 ,或者在中间带有连字符的数字, 123-489-568 ,或者只是普通的正常数字123456789 - 但它不能匹配没有数字的任何内容。 Thanks, 谢谢,

Better example of what I want (in bold) in a sample text: 在示例文本中我想要的更好的例子(粗体):

ABC1 ABC 23-4787 ABCD 4578 ABCD 11/01/2014 ABREKF ABC1 ABC 23-4787 ABCD 4578 ABCD 11/01/2014 ABREKF

There must be something better, but I think this should work: 必须有更好的东西,但我认为这应该有效:

\S*\d+\S*

\\S* - Zero or more non-whitespace characters \\S* - 零个或多个非空白字符

\\d+ - One or more digits \\d+ - 一个或多个数字

\\S* - Zero or more non-whitespace characters \\S* - 零个或多个非空白字符

Use this lookahead: 使用这个前瞻:

(?=\D*\d)

This asserts that the string contains any quantity of non numeric characters ( \\D ) followed by a single digit. 这断言该字符串包含任意数量的非数字字符( \\D )后跟一个数字。

If you want to match/capture the string, then just add .* to the regex: 如果你想匹配/捕获字符串,那么只需将.*添加到正则表达式:

(?=\D*\d).*

Reference: http://www.rexegg.com/regex-lookarounds.html 参考: http//www.rexegg.com/regex-lookarounds.html

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

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