正则表达式匹配两个具有相同长度的不同组
regex to match two different groups with the same length
我想构建一个匹配两组的正则表达式,第二组由单个字符组成,重复次数与第一组中的字符数相同。像 ^(\w+) (x{length of })
这样的东西,例如 hello xxxxx
和 foo xxx
会匹配,但 hello xxxxy
和 foo xxy
不会匹配。这可能吗?
此处的目标是匹配 reStructuredText 样式列表中的缩进,其中列表项中的第二行应缩进以匹配第一行中文本的开头,不包括可变长度数字列表标记。例如,
1. If the number is small,
subsequent lines are typically
indented three spaces.
2. Although if the first line has
multiple leading spaces then
subsequent lines should reflect that.
11. And when the number starts to get
bigger the indent will necessarily
be bigger too.
如果
你可以做到
- 您的正则表达式引擎支持条件模式并且
- 您愿意接受固定的重复次数上限。
在这种情况下,您可以这样做:
^(\w)?(\w)?(\w)?(\w)?(\w)? (?(1)x)(?(2)x)(?(3)x)(?(4)x)(?(5)x)
此示例最多匹配 5 个长度。
我想构建一个匹配两组的正则表达式,第二组由单个字符组成,重复次数与第一组中的字符数相同。像 ^(\w+) (x{length of })
这样的东西,例如 hello xxxxx
和 foo xxx
会匹配,但 hello xxxxy
和 foo xxy
不会匹配。这可能吗?
此处的目标是匹配 reStructuredText 样式列表中的缩进,其中列表项中的第二行应缩进以匹配第一行中文本的开头,不包括可变长度数字列表标记。例如,
1. If the number is small,
subsequent lines are typically
indented three spaces.
2. Although if the first line has
multiple leading spaces then
subsequent lines should reflect that.
11. And when the number starts to get
bigger the indent will necessarily
be bigger too.
如果
你可以做到- 您的正则表达式引擎支持条件模式并且
- 您愿意接受固定的重复次数上限。
在这种情况下,您可以这样做:
^(\w)?(\w)?(\w)?(\w)?(\w)? (?(1)x)(?(2)x)(?(3)x)(?(4)x)(?(5)x)
此示例最多匹配 5 个长度。