Regex 用于匹配同一字符串中的单词和数字的正则表达式
我不知道正则表达式,需要找到表达式来隔离包含单词“comp”加上任何价格(数字)的字符串 有什么想法吗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
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”开头的