Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 从匹配的字符串中提取子字符串_Regex_Python 2.7 - Fatal编程技术网

Regex 从匹配的字符串中提取子字符串

Regex 从匹配的字符串中提取子字符串,regex,python-2.7,Regex,Python 2.7,在字符串开头匹配24后,我试图从字符串中提取子字符串。子字符串是一个MAC id,从位置6开始,一直到字符串的末尾。我知道子字符串方法可以完成这项工作。我很想知道正则表达式的实现 字符串=2410100:80:a3:bf:72:d45 经过多次尝试和错误,这是我的注册码,我认为是复杂的 [^24*$](?<=^\S{6}).*$ [^24*$](?您可以使用: (?<=^24\S{3}).*$ 演示: 细分: (?<= # Start of a pos

在字符串开头匹配
24
后,我试图从字符串中提取子字符串。子字符串是一个MAC id,从位置6开始,一直到字符串的末尾。我知道子字符串方法可以完成这项工作。我很想知道正则表达式的实现

字符串=
2410100:80:a3:bf:72:d45

经过多次尝试和错误,这是我的注册码,我认为是复杂的

[^24*$](?<=^\S{6}).*$
[^24*$](?您可以使用:

(?<=^24\S{3}).*$
演示:

细分:

(?<=            # Start of a positive Lookbehind.
    ^           # Asserts position at the beginning of the string.
    24          # Matches `24` literally.
    \S{3}       # Matches any three non-whitespace characters.
)               # End of the Lookbehind (five characters so far).
(?:             # Start of a non-capturing group.
    [0-9a-f]    # A number between `0` and `9` or a letter between `a` and `f` (at pos. #6).
    {2}         # Matches the previous character class exactly two times.
    :           # Matches `:` literally.
)               # End of the non-capturing group.
{5}             # Matches the previous group exactly five times.
[0-9a-f]        # Any number between `0` and `9` or any letter between `a` and `f`.
{2}             # Matches the previous character class exactly two times.

(?
(?我知道了。不用担心。
24
匹配前两个字符(分别是“2”和“4”)。
\S{3}
匹配接下来的三个非空白字符。到目前为止,我们已经使用了五个字符,因此下一个字符位于位置#6。有关更多信息,请参阅下面的答案。您是对的。它最多只能使用
4
。这是一个复制/粘贴错误。艾哈迈德,非捕获组的目的是什么?我没有看到多个组重新执行rned。我正在试图理解字符串中的哪里应用了
?:
。@RSSregex“我没有看到返回多个组”这是因为它是一个非捕获组。“非捕获组的用途是什么?”应该回答您的问题;但简单地说,当您需要对比赛的一部分进行分组,但不想单独捕获比赛时,通常会使用非捕获组。您可能需要对比赛的一部分进行分组的原因有很多(例如,重复、使其可选、交替等)。在我们的示例中,我们需要重复该组(这是使用
{5}
完成的)。
(?<=            # Start of a positive Lookbehind.
    ^           # Asserts position at the beginning of the string.
    24          # Matches `24` literally.
    \S{3}       # Matches any three non-whitespace characters.
)               # End of the Lookbehind (five characters so far).
(?:             # Start of a non-capturing group.
    [0-9a-f]    # A number between `0` and `9` or a letter between `a` and `f` (at pos. #6).
    {2}         # Matches the previous character class exactly two times.
    :           # Matches `:` literally.
)               # End of the non-capturing group.
{5}             # Matches the previous group exactly five times.
[0-9a-f]        # Any number between `0` and `9` or any letter between `a` and `f`.
{2}             # Matches the previous character class exactly two times.