The RegExp non-capturing group (?:x) used to match x but does not include the match in the result. The matched substring cannot be recalled from the resulting list elements or from the predefined RegExp object's properties.
The non capturing group matches with the given value but rather storing the matching group in the result, it (non-capturing groups) will be excluded from the result. In other word, the string validated against the the given regex but non-capturing group will be excluded from the result.
1 import re
2
3 string = "employee/10"
4
5 res = re.findall("(\w+)\/(?:\d+)", string)
6
7 print("Result :", res)
In the above example, a string define with path and regex object is created by specifying an non-capturing group. It validate the given string against regex which should match against regex and includes both group in the string.
As we have specified id group as non-capturing group, it validate the specified string that it must have id as number but it will not include in the result of findall() method. Hence, it only returns an array with two elements, index 0, contain regex match result and index 1 contain first group value.
Invalid source string for non-capturing group
Invalid source string for non-capturing group
1 import re
2
3 string = "employee/test"
4
5 res = re.findall("(\w+)\/(?:\d+)", string)
6
7 print("Result :", res)
The above example prints null as source string not match with the given regex. Even though we have specified id as non capturing group, it will be validated against regex but it will be excluded from the result.
Related options for your search