有没有一种匹配模式或字符串开头的好方法?

Is there a good way to match a pattern or at beginning of a string?

我想找到所有数字:

  1. 在“#”之后
  2. 开头

例如,

>>> s = '1___2___#3___@4___##5'

结果应该是

['1', '3', '5']

我现在拥有的是

>>> re.findall('#(\d)', s)          # ['3', '5']

>>> re.findall('^(\d)', s)          # ['1']

但我不知道如何将它们组合成一个正则表达式。感谢您的帮助。

保持简单...

re.findall 优先捕获组。所以把前面的^starting anchor)和#放在非捕获组里面。

>>> s = '1___2___#3___@4___##5'
>>> re.findall('(?:^|#)(\d+)', s)
['1', '3', '5']

简单多了..

>>> s = '1___2___#3___@4___##5'
>>> re.findall('(?<![^#])\d+', s)
['1', '3', '5']

DEMO

上面的正则表达式是这样工作的...

  • (?<!.)\d+ 匹配所有前面没有字符的数字(换行符除外)。所以这必须匹配开始时出现的数字,因为开始时只满足这个条件。

  • (?<![^#])\d+ 再进一步,这个正则表达式将匹配开始时出现的数字,因为 [^#] 消耗一个字符,它也匹配所有的数字前面没有不属于 # 的字符。

^\d+|(?<=#)\d+

您可以尝试 this.See 演示。

https://regex101.com/r/sH8aR8/51

使用

re.findall('^\d+|(?<=#)\d+', s)  

使用 0 width assertions.

仅捕获您需要的内容
NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                       the most amount possible))
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    #                        '#'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                       the most amount possible))