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

发布于 2024-07-14 18:29:34 字数 412 浏览 9 评论 0原文

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

[$]\d+

这给了我 4 个匹配项,

$10 $10 $20a a$20

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

[$]\d+\b

但它再次

为我匹配了 a$20。

我尝试过

\b[$]\d+\b

,但失败了。

我正在寻找说,仅当单词以 $ 开头且后跟数字时才接受。 我如何告诉它以 $ 开头,因为我认为 \b 使其假定单词边界,这意味着包围在字母数字字符内。

解决办法是什么?

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

$10 $10 $20a a$20

so I thought of using word boundaries using \b:

[$]\d+\b

But it again matched

a$20 for me.

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.

What is the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吐个泡泡 2024-07-21 18:29:35

这不是最好的解决方案,但这应该可行。 (它与你的测试用例有关)

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

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

(?<=\s+|^)\$\d+\b
﹎☆浅夏丿初晴 2024-07-21 18:29:35

你有没有尝试过

\B\$\d+\b

Have you tried

\B\$\d+\b

十秒萌定你 2024-07-21 18:29:35

你已经很接近了,你只需要转义 $:

\B\$\d+\b

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

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

香橙ぽ 2024-07-21 18:29:35

尝试使用 ^\$\d+,

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

Try with ^\$\d+

where ^ denoted the beginning of a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文