Python正则表达式:拒绝一个两位数并接受其他两位数

1 投票
6 回答
2987 浏览
提问于 2025-04-16 16:26

我想知道怎么写一个正则表达式,让它不匹配某个特定的数字,比如说不匹配13,但其他的数字都可以匹配。下面的第二行代码不行,因为它连10、11这些数字也匹配不上。而第三行代码则是想找一个不以1开头但里面有3的数字。

str = 'val=13'

regex = 'val=[^1][^3]'
regex = 'val=[^13]

re.search(regex, str)

6 个回答

3

为什么需要正则表达式?

>>> string = 'val=13'
>>> array=string.split("=")
>>> if array[0] == "val" and int(array[1]) < 100 and int(array[1]) == 13
...     print "match"
...
>>>

而且这些都是在假设我理解你写的内容。

6

使用前瞻断言:

In : re.findall('val=(?!13$)(\d{2,})', 'val=12') 
Out: ['12']

In : re.findall('val=(?!13$)(\d{2,})', 'val=13') 
Out: []
0

编辑: 修改为匹配一个或多个数字的情况。

如果你需要提取各种形式的 val=dd,其中 dd 必须是一个包含一个或多个数字的数,而且这个数字不能是 13(或者 -13),同时等号周围可能有空格,数字前面可以有可选的 +/- 符号,那么这个脚本展示了一种实现方法:

import re
# Regex: VERBOSE, commented version.
reLong = r"""
    # Match "val=dd" where dd has two or more digits (but is not 13).
    \bval\s*=\s*  # "val=" literal with optional ws.
    (?!           # Begin negative lookahead assertion.
      [+\-]?      # Number may have optional sign.
      13          # This is the number we don't match
      \b          # which ends on a word boundary.
    )             # The number is not 13, +13 or -13.
    (             # $1: Two digit number.
      [+\-]?      # Allow optional sign.
      \d+         # One or more digits.
    )             # End $1: containing 2 digit number
    """
# Regex: short, uncommented version.
reShort = r"\bval\s*=\s*(?![+\-]?13\b)([+\-]?\d{2}\b)"

# Compile a regex object we can use over and over.
reObj = re.compile(reLong, re.IGNORECASE | re.VERBOSE)

# Test data strings. Positive and negative. With/without whitespace.
input = [
    # String with positive numbers. No whitespace.
    "val=1 val=13, val=133, val=12, otherval=1",
    # String with positive numbers. With whitespace.
    "val = 1 val = 13, val = 133, val = 12, otherval = 1",
    # String with negative numbers. No whitespace.
    "val=-1 val=-13, val=-133, val=-12, otherval=-1",
    # String with negative numbers. With whitespace.
    "val = -1 val = -13, val = -133, val = -12, otherval = -1",
    ]

# Parse all test data strings and print matches.
for str in input:
    matches = reObj.findall(str)
    if matches:
        print(matches)

正则表达式以简短和详细的形式呈现,并附有注释。以下是脚本的输出结果:

['1', '133', '12']
['1', '133', '12']
['-1', '-133', '-12']
['-1', '-133', '-12']

补充: 为了匹配那些不是超过一个字符的内容,我们使用了 负向前瞻。例如,如果想匹配不是 "BAD" 的单词,这里有一个带注释的正则表达式,展示了如何做到这一点:

reobj = re.compile(r"""
    # Match a word that is not: 'BAD'
    \b         # Anchor to start of word.
    (?!BAD\b)  # Verify that this word is not 'BAD'
    \w+        # Ok. Safe to match non-BAD word.
    \b         # Anchor to end of word.
    """, re.VERBOSE)

撰写回答