Perl Regex:区分强制性和可选字符
问题是关于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
逐字了解此要求,您可以使用此Regex :
该代表:
&lt;
:匹配&lt;
[\ d,\ h]*
:匹配0或更多数字或whitespace或whitespace或逗号\ d
:匹配数字[\ d,\ h]*
:匹配0或更多数字或whitespace或whitespace或comma&gt;
:匹配a&gt;
regex demo
Verbatim for this requirement, you can use this regex:
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
您可以使用
regex demo 。 详细信息:
&lt;
- a&lt;
char\ d+
- 一个或多个,
,零或更多whitespaces,然后是一个或多个数字&gt;
- a <代码>&gt; char。You can use
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.