正则表达式python3

ind*_*iag 1 python regex

我尝试学习python3,我与正则表达式堆叠一点.我为此研究了HOWTO,但我不太了解.这页

    1\d2\D2
    ^a\w+z$
Run Code Online (Sandbox Code Playgroud)

and*_*oke 9

您可以通过读取表达式并逐步选择适当的字符来生成示例字符串.

例如,1\d2\D2:

1\d2\D2 -> 1
^ 1 means a literal number 1

1\d2\D2 -> 17
 ^^ \d means any digit (0-9).  let's choose 7.

1\d2\D2 -> 172
   ^ 2 means a literal number 2.

1\d2\D2 -> 172X
    ^^ \D means anything *but* a digit (0-9).  let's choose X

1\d2\D2 -> 172X2
      ^ 2 means a literal number 2.

所以172X2会匹配1\d2\D2

你的下一个 - ^a\w+z$可以有多种长度:

^a\w+z$
^ this means we need to be at the start of a line (and we are, so that's cool)

^a\w+z$ -> a
 ^ a means a literal letter a

^a\w+z$ -> a4
  ^^ \w means a digit, letter, or "_".  let's choose 4.

^a\w+z$ -> a4
    ^ + means we can return to whatever is to the left, if we want, so let's do that...

^a\w+z$ -> a4Q
  ^^ \w means a digit, letter, or "_".  let's choose Q.

^a\w+z$ -> a4Q
    ^ + means we can return to whatever is to the left, if we want, so let's do that...

^a\w+z$ -> a4Q1
  ^^ \w means a digit, letter, or "_".  let's choose 1.

^a\w+z$ -> a4Q1
    ^ + means we can return to whatever is to the left, but now let's stop

^a\w+z$ -> a4Q1z
     ^ z means a literal letter z

^a\w+z$ -> a4Q1z
      ^ $ means we must be at the end of the line, and we are (and so cannot add more)

所以a4Q1z会匹配^a\w+z$.那么a4z(你可以检查......)

注意,*就像是+在你能跳回来,重复,但意味着你可以完全跳过什么是向左(换句话说,+表示"重复播放至少一次",而是*指"重复零个或多个"(以下简称"零"正在跳过").

更新:

[abc]意思是选择任何一个a,bc.

x{2,3}意味着添加x2到3次(比如+但是限制次数).所以,xxxxx.

\1有点复杂.你需要找到第一个(因为数字1)括号内的内容并添加它.因此,例如,(\d+)\1将匹配2323,如果你已经从左侧努力权和选择23(\d+).