Perl Regex:区分强制性和可选字符

发布于 2025-02-08 19:35:31 字数 529 浏览 2 评论 0原文

问题是关于Perl正则表达式。

字符串

  • 我需要匹配以<开头的
  • ,并以>结尾必须包含数字[0-9]
  • 可以选择包含\ s(IE空间)和
  • 任何数量的字符
  • 订单是随机的

,此模式不会区分强制性和可选字符:

/<[0-9,\s]+>/

并且将匹配:

<9>
<9,10>
<9, 10>

这是我想要的,但我不想要的这两个:

< >
<,>

那么,如何设置一个将找到一个将始终包含0-9并可以选择包含\ s,的匹配的Perl Regex?

The question is about PERL regular expressions.

I need to match a string that

  • starts with < and ends with >
  • must contain number [0-9]
  • can optionally contain \s (i.e. space) and ,
  • any number of characters
  • order is random

This pattern does not discriminate between mandatory and optional characters:

/<[0-9,\s]+>/

and will match:

<9>
<9,10>
<9, 10>

which is what I want, but also these two that I dont want:

< >
<,>

So, how to set a PERL regex that will find a match that will always contain 0-9 and can optionally contain \s, ?

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

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

发布评论

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

评论(2

天生の放荡 2025-02-15 19:35:31

如何设置Perl Regex,该perl Regex将找到一个将始终包含0-9的匹配项,并且可以选择包含\ s,

逐字了解此要求,您可以使用此Regex :

/<[\d,\h]*\d[\d,\h]*>/

该代表:

  • &lt;:匹配&lt;
  • [\ d,\ h]*:匹配0或更多数字或whitespace或whitespace或逗号
  • \ d:匹配数字
  • [\ d,\ h]*:匹配0或更多数字或whitespace或whitespace或comma
  • &gt;:匹配a &gt;

regex demo

how to set a PERL regex that will find a match that will always contain 0-9 and can optionally contain \s,:

Verbatim for this requirement, you can use this regex:

/<[\d,\h]*\d[\d,\h]*>/

Which stands for:

  • <: Match a <
  • [\d,\h]*: Match 0 or more digits or whitespace or comma
  • \d: Match a digit
  • [\d,\h]*: Match 0 or more digits or whitespace or comma
  • >: Match a >

RegEx Demo

小兔几 2025-02-15 19:35:31

您可以使用

<\d+(?:,\s*\d+)*>

regex demo 详细信息

  • &lt; - a &lt; char
  • \ d+ - 一个或多个
  • 数字,\ s*\ d+)* - 零或更多的出现,零或更多whitespaces,然后是一个或多个数字
  • &gt; - a <代码>&gt; char。

You can use

<\d+(?:,\s*\d+)*>

See the regex demo. Details:

  • < - a < char
  • \d+ - one or more digits
  • (?:,\s*\d+)* - zero or more occurrences of a ,, zero or more whitespaces and then one or more digits
  • > - a > char.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文