查找严格以$开头的单词,Regex C#

时间:2022-05-05 21:37:54

I need to find all matches of word which strictly begins with "$" and contains only digits. So I wrote

我需要找到严格以“$”开头且仅包含数字的所有单词匹配。所以我写了

[$]\d+

which gave me 4 matches for

这给了我4场比赛

$10 $10 $20a a$20

so I thought of using word boundaries using \b:

所以我想到使用\ b来使用单词边界:

[$]\d+\b

But it again matched

但它再次匹配

a$20 for me.

给我20美元。

I tried

\b[$]\d+\b

but I failed.

但我失败了。

I'm looking for saying, ACCEPT ONLY IF THE WORD STARTS WITH $ and is followed by DIGITS. How do I tell IT STARTS WITH $, because I think \b is making it assume word boundaries which means surrounded inside alphanumeric characters.

我正在寻求说,只有在WORD以$开头并接着是DIGITS时才接受。我如何告诉IT STARTS WITH $,因为我认为\ b使它假设单词边界,这意味着包围在字母数字字符内。

What is the solution?

解决办法是什么?

4 个解决方案

#1


Not the best solution but this should work. (It does with your test case)

不是最好的解决方案,但这应该有效。 (它适用于您的测试用例)

(?<=\s+|^)\$\d+\b

#2


Have you tried

你有没有尝试过

\B\$\d+\b

#3


You were close, you just need to escape the $:

你很亲密,你只需要逃避$:

\B\$\d+\b

See the example matches here: http://regexhero.net/tester/?id=79d0ac3b-dd2c-4872-abb4-6a9780c91fc1

请参阅此处的示例匹配项:http://regexhero.net/tester/?id = 79d0ac3b-dd2c-4872-abb4-6a9780c91fc1

#4


Try with ^\$\d+

尝试使用^ \ $ \ d +

where ^ denoted the beginning of a string.

其中^表示字符串的开头。

#1


Not the best solution but this should work. (It does with your test case)

不是最好的解决方案,但这应该有效。 (它适用于您的测试用例)

(?<=\s+|^)\$\d+\b

#2


Have you tried

你有没有尝试过

\B\$\d+\b

#3


You were close, you just need to escape the $:

你很亲密,你只需要逃避$:

\B\$\d+\b

See the example matches here: http://regexhero.net/tester/?id=79d0ac3b-dd2c-4872-abb4-6a9780c91fc1

请参阅此处的示例匹配项:http://regexhero.net/tester/?id = 79d0ac3b-dd2c-4872-abb4-6a9780c91fc1

#4


Try with ^\$\d+

尝试使用^ \ $ \ d +

where ^ denoted the beginning of a string.

其中^表示字符串的开头。