Regex以匹配特定长度的数字

时间:2021-03-01 23:36:56

I am looking to match a 15 digit number (as part of a larger regex string). Right now, I have

我希望匹配一个15位数(作为更大regex字符串的一部分)。现在,我有

\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d

but I feel like there must be a cleaner way to do this.

但我觉得必须有一个更干净的方法。

3 个解决方案

#1


23  

If your regex language is Perl-compatible: \d{15}.

如果您的regex语言是perl兼容的:\d{15}。

It is difficult to say how handle the edges (so you don't accidentally grab extra digits) without knowing the outer context in which this snippet will be used. The definitive context-independent solution is this:

如果不知道要使用这个代码片段的外部上下文,很难说如何处理这些边(这样您就不会意外地获取额外的数字)。与上下文无关的最终解决方案是:

(?:(?<!\d)\d{15}(?!\d))

You can put this in the middle of any regex and it will match (and only match) a sequence of exactly 15 digits. It is, however, quite awkward, and usually unnecessary. A simpler version that assumes non-alphanumeric boundaries (e.g., whitespace around the digits) is this:

您可以将它放在任何regex的中间,它将匹配(且仅匹配)一个恰好为15位的序列。然而,这是相当尴尬的,而且通常是不必要的。一个更简单的假设非字母数字边界(例如,围绕数字的空格)的版本是:

(?:\b\d{15}\b)

But it won't work if the letters immediately precede or followed the sequence.

但是如果字母在序列的前面或后面,它就不能工作。

In both of the above cases, the outer (?:...) is just a bracketing construct to avoid precedence problems with the surrounding regex. Whether it is required also depends on the context.

在上述两种情况下,外部(?:…)只是一个括号结构,以避免与周围regex发生优先级问题。是否需要它也取决于上下文。

#2


27  

You can generally do ranges as follows:

你通常可以做如下的范围:

\d{4,7}

which means a minimum of 4 and maximum of 7 digits. For your particular case, you can use the one-argument variant, \d{15}.

这意味着至少4位,最多7位。对于您的特定情况,您可以使用单参数变体,\d{15}。

Both of these forms are supported in Python's regular expressions - look for the text {m,n} at that link.

在Python的正则表达式中支持这两种形式——在该链接中查找文本{m,n}。

And keep in mind that \d{15} will match fifteen digits anywhere in the line, including a 400-digit number. If you want to ensure it only has the fifteen, you use something like:

记住,\d{15}将匹配行中任何位置的15位数字,包括400位数字。如果您想确保它只有15个,您可以使用以下内容:

^\d{15}$

which uses the start and end anchors, or

使用开始和结束锚,还是

^\D*\d{15}\D*$

which allows arbitrary non-digits on either side.

它允许任意的非数字在两边。

#3


0  

There, are two ways i have, to limit numbers.

有两种方法可以限制数字。

using len,

使用len,

num = 1234
len(str(num)) <= 4

This output will be True / False.

这个输出将是True / False。

using regular expression,

使用正则表达式,

import re
num = 12324
re.match(r'(?:(?<!\d)\d{4}(?!\d))', str(num))

The output will be regular expression object or None.

输出将是正则表达式对象或无。

#1


23  

If your regex language is Perl-compatible: \d{15}.

如果您的regex语言是perl兼容的:\d{15}。

It is difficult to say how handle the edges (so you don't accidentally grab extra digits) without knowing the outer context in which this snippet will be used. The definitive context-independent solution is this:

如果不知道要使用这个代码片段的外部上下文,很难说如何处理这些边(这样您就不会意外地获取额外的数字)。与上下文无关的最终解决方案是:

(?:(?<!\d)\d{15}(?!\d))

You can put this in the middle of any regex and it will match (and only match) a sequence of exactly 15 digits. It is, however, quite awkward, and usually unnecessary. A simpler version that assumes non-alphanumeric boundaries (e.g., whitespace around the digits) is this:

您可以将它放在任何regex的中间,它将匹配(且仅匹配)一个恰好为15位的序列。然而,这是相当尴尬的,而且通常是不必要的。一个更简单的假设非字母数字边界(例如,围绕数字的空格)的版本是:

(?:\b\d{15}\b)

But it won't work if the letters immediately precede or followed the sequence.

但是如果字母在序列的前面或后面,它就不能工作。

In both of the above cases, the outer (?:...) is just a bracketing construct to avoid precedence problems with the surrounding regex. Whether it is required also depends on the context.

在上述两种情况下,外部(?:…)只是一个括号结构,以避免与周围regex发生优先级问题。是否需要它也取决于上下文。

#2


27  

You can generally do ranges as follows:

你通常可以做如下的范围:

\d{4,7}

which means a minimum of 4 and maximum of 7 digits. For your particular case, you can use the one-argument variant, \d{15}.

这意味着至少4位,最多7位。对于您的特定情况,您可以使用单参数变体,\d{15}。

Both of these forms are supported in Python's regular expressions - look for the text {m,n} at that link.

在Python的正则表达式中支持这两种形式——在该链接中查找文本{m,n}。

And keep in mind that \d{15} will match fifteen digits anywhere in the line, including a 400-digit number. If you want to ensure it only has the fifteen, you use something like:

记住,\d{15}将匹配行中任何位置的15位数字,包括400位数字。如果您想确保它只有15个,您可以使用以下内容:

^\d{15}$

which uses the start and end anchors, or

使用开始和结束锚,还是

^\D*\d{15}\D*$

which allows arbitrary non-digits on either side.

它允许任意的非数字在两边。

#3


0  

There, are two ways i have, to limit numbers.

有两种方法可以限制数字。

using len,

使用len,

num = 1234
len(str(num)) <= 4

This output will be True / False.

这个输出将是True / False。

using regular expression,

使用正则表达式,

import re
num = 12324
re.match(r'(?:(?<!\d)\d{4}(?!\d))', str(num))

The output will be regular expression object or None.

输出将是正则表达式对象或无。