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

时间:2018-03-29 03:44:45

标签: 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.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

2 个答案:

答案 0 :(得分:1)

尝试

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

\bcomp\b用于边界词comp

.+代表一到多个字符。

[0-9]*[.]?[0-9]+表示浮点数。

| for或condition。 number | compcomp | number

答案 1 :(得分:0)

试试这个

/ \ d?+排版\ d?+ /克

它会将所有字符串“comp”与数字匹配。我觉得它适合你