正则表达式匹配名称后跟数字(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

正则表达式匹配名称后跟数字(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