Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用于匹配同一字符串中的单词和数字的正则表达式_Regex - Fatal编程技术网

Regex 用于匹配同一字符串中的单词和数字的正则表达式

Regex 用于匹配同一字符串中的单词和数字的正则表达式,regex,Regex,我不知道正则表达式,需要找到表达式来隔离包含单词“comp”加上任何价格(数字)的字符串 有什么想法吗 249.00 | 259.00 | 279.00 | comp | 349.00 | //I need to return this as match 369.00 | 359.00 | 599.00 | //don't want to return this as match 299.00 | 499.00 | //don't want to return this as match 329

我不知道正则表达式,需要找到表达式来隔离包含单词“comp”加上任何价格(数字)的字符串

有什么想法吗

249.00 | 259.00 | 279.00 | comp | 349.00 | //I need to return this as match
369.00 | 359.00 | 599.00 | //don't want to return this as match
299.00 | 499.00 | //don't want to return this as match
329.00 | //don't want to return this as match
comp | 269.00 | 269.00 | //I need to return this as match
179.00 | 239.00 | comp | //I need to return this as match
comp | //don't want to return this as match
89.00 | 89.00 | 89.00 | //no match
249.00 | //don't want to return this as match
comp | 249.00 | //I need to return this as matc
199.00 | comp | comp | //I need to return this as match
comp | comp | 99.00 | 99.00 | //I need to return this as match
comp | comp | comp | comp | comp | //I need to return this as match
让我们试试这个

/\d?+comp\d?+/g

它将所有字符串“comp”与数字匹配。我认为这对你很合适

试试看

(\bcomp\b.+([0-9]*[.]?[0-9]+)|(([0-9]*[.]?[0-9]+).+\bcomp\b)

\bcomp\b
用于边界词
comp

+
用于一对多字符

[0-9]*[.]?[0-9]+
用于浮点数


|
用于或条件
number | comp
comp | number

我试过了,但它给了我只有comp的字符串,我需要comp+数字/comp/gm应该有效为什么
comp |
comp | comp | comp | comp |
不匹配?@meebow:谢谢。我改成了这个(\d?)(comp\d?),它可以工作,但不包括那些以“comp”开头的