正式表达在遇到的首字母中停止
我希望我的正则表达式在遇到字母后停止匹配2到10之间的长度。
到目前为止,我想出了(\ d {2,10})(?![A-ZA-Z])
this。但是即使在遇到字母之后,它仍继续匹配。 2216101225/roc/pl fct DIN 24.03.2022 pl erbicide'
- 这是我一直在测试正则时的文本,但它也与24 03和2022匹配。 这是对C#进行测试和旨在的。
你能帮忙吗?谢谢
I want my regex expression to stop matching numbers of length between 2 and 10 after it encounters a letter.
So far I've come up with (\d{2,10})(?![a-zA-Z])
this. But it continues to match even after letters are encountered.2216101225 /ROC/PL FCT DIN 24.03.2022 PL ERBICIDE'
- this is the text I've been testing the regex on, but it matches 24 03 and 2022 also.
This is tested and intended for C#.
Can you help ? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一个选择是固定图案并匹配除chars a-za-z或newline以外的任何字符,然后在捕获组中捕获2-10位数字。
然后从比赛中获取捕获组1值。
说明
^
String的启动[^a-Za-Z \ r \ n] newline
\ b([0-9] {2,10})\ b
捕获第1组中的单词边界之间的2-10位数字,请参见a Regex Demo 。
请注意,在.net
\ d
中匹配所有数字,但仅0-9。Another option is to anchor the pattern and to match any character except chars a-zA-Z or a newline, and then capture the 2-10 digits in a capture group.
Then get the capture group 1 value from the match.
Explanation
^
Start of string[^A-Za-z\r\n]*
Optionally match chars other than a-zA-Z or a newline\b([0-9]{2,10})\b
Capture 2-10 digits between word boundaries in group 1See a regex demo.
Note that in .NET
\d
matches all numbers except for only 0-9.您可以使用以下.NET正则表达式
请参见 regex demo 。 详细信息:
(? < =^[^a-za-z]*)
仅支持ascii字母)(?<!\ d)
- 允许立即在左侧的数字。\ d {2,10}
- 两到十位数(?!\ d)
- 允许立即在右边的数字。You can use the following .NET regex
See the regex demo. Details:
(?<=^\P{L}*)
- there must be no letters from the current position till the start of string ((?<=^[^a-zA-Z]*)
only supports ASCII letters)(?<!\d)
- no digit immediately on the left is allowed.\d{2,10}
- two to ten digits(?!\d)
- no digit immediately on the right is allowed.