查找严格以 $ 开头的单词,Regex C#
我需要找到严格以“$”开头且仅包含数字的单词的所有匹配项。 所以我写了
[$]\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是最好的解决方案,但这应该可行。 (它与你的测试用例有关)
Not the best solution but this should work. (It does with your test case)
你有没有尝试过
Have you tried
你已经很接近了,你只需要转义 $:
请参阅此处的示例匹配: http://regexhero.net/tester/?id=79d0ac3b-dd2c-4872-abb4-6a9780c91fc1
You were close, you just need to escape the $:
See the example matches here: http://regexhero.net/tester/?id=79d0ac3b-dd2c-4872-abb4-6a9780c91fc1
尝试使用 ^\$\d+,
其中 ^ 表示字符串的开头。
Try with ^\$\d+
where ^ denoted the beginning of a string.