What is the use of non capturing group?

import re pattern = r"(?P<first>abc)(?:def)(ghi)" match = re.match(pattern, "abcdefghi") if match: print(match.group("first")) print(match.groups())

5/31/2019 2:00:36 PM


1 Answer

New Answer


You can use a non-capturing group if you need to group something but you're not interested in the content. For example a URL needs to start with either http:// or https://. You don't care which one it is, it just has to be there. So you can use a non-capturing group like this: r'(?:https?)://(www\\..+\\.com)' This pattern has two groups. A non-capturing group that matches either http:// or https:// and a capturing group for the actual URL.