正则表达式匹配名称后跟数字(Regex for matching a name followed by numbers)

编程入门 行业动态 更新时间:2024-10-28 06:29:18
正则表达式匹配名称后跟数字(Regex for matching a name followed by numbers)

我对此任务的问题是:

如果名称和数字之间有:或/ ,则可以在符号后面加上空格

如果没有符号,则名称和数字之间必须有空格。

到目前为止,我来到这里:

^\s*[^/:\d]*\s*?[/:]?\s*([0-9]{6,10})\s*$

但那匹配Name1234567错了。 这应该是错的,但这是真的。 (之间必须有空格)

1234567 , Name 1234567或Name/1234567一切都很好。 但我不能让那个特定的任务起作用。

希望有人能解决这个问题。 格尔茨

The problem i have with this task is:

If there is a : or / between the name and the number there CAN be a whitespace bevore and after the symbol

If there is non of the symbols there MUST be a whitespace between the name and the number.

So far i came to this:

^\s*[^/:\d]*\s*?[/:]?\s*([0-9]{6,10})\s*$

But that matches Name1234567 wrong. It should be wrong but is true. (there MUST be a whitespace between)

1234567, Name 1234567 or Name/1234567 works everything perfectly fine. But i can't get that one specific task working.

Hope someone can fix that problem. Greetz

最满意答案

怎么样

[a-zA-Z]*(?:\s|(?:\s?[:\/]\s?))\d+

例如http://regex101.com/r/dD3lP4/1

它是如何匹配的

[a-zA-Z]*匹配任意数量的字母

(?:\s|(?:\s?[:\/]\s?))匹配空格\s或\s?[:\/]\s? 空格后跟:或/后跟空格\ s。 量化? 使得出现为零或一个(可以在问题中)

\d+匹配任意数量的数字

How about

[a-zA-Z]*(?:\s|(?:\s?[:\/]\s?))\d+

for example http://regex101.com/r/dD3lP4/1

How it matches

[a-zA-Z]* matches any number of alphabets

(?:\s|(?:\s?[:\/]\s?))matches a space \s or \s?[:\/]\s? space followed by : or / again followed by space \s. The quantifies ? makes \s occurence as zero or one (can part in the question)

\d+ matches any number of digits

更多推荐

本文发布于:2023-08-07 22:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后跟   名称   数字   正则表达式   numbers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!