正式表达在遇到的首字母中停止

发布于 2025-01-29 19:01:24 字数 248 浏览 3 评论 0原文

我希望我的正则表达式在遇到字母后停止匹配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 技术交流群。

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

发布评论

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

评论(2

人间不值得 2025-02-05 19:01:24

另一个选择是固定图案并匹配除chars a-za-z或newline以外的任何字符,然后在捕获组中捕获2-10位数字。

然后从比赛中获取捕获组1值。

^[^A-Za-z\r\n]*\b([0-9]{2,10})\b

说明

  • ^ 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.

^[^A-Za-z\r\n]*\b([0-9]{2,10})\b

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 1

See a regex demo.


Note that in .NET \d matches all numbers except for only 0-9.

自在安然 2025-02-05 19:01:24

您可以使用以下.NET正则表达式

(?<=^\P{L}*)(?<!\d)\d{2,10}(?!\d)
(?<=^[^a-zA-Z]*)(?<!\d)\d{2,10}(?!\d)

请参见 regex demo 详细信息

  • (? &lt; =^[^a-za-z]*)仅支持ascii字母)
  • (?&lt;!\ d) - 允许立即在左侧的数字。
  • \ d {2,10} - 两到十位数
  • (?!\ d) - 允许立即在右边的数字。

You can use the following .NET regex

(?<=^\P{L}*)(?<!\d)\d{2,10}(?!\d)
(?<=^[^a-zA-Z]*)(?<!\d)\d{2,10}(?!\d)

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