# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<class>^(?<fully_qualified_class_name>(?<deliniator>(?<!\\)\\(?!\\)(?=(?P>fully_qualified_class_name))){0,1}[A-z_][\w]*)(?=::(?P>method)|$)|^(?<namespace>^(?P>fully_qualified_class_name)*)(?P>deliniator)(?P>fully_qualified_class_name)(?=::(?P>method)|$))(?<scope_resolution_operator>::(?=(?P>method)(?!$)))?(?<method>(?<=::)(?<operation>(?<mutator>[sS]et(?=[A-Z0-1_]|$))|(?<accessor>[gG]et(?=[A-Z0-1_]|$))|(?<condition>[iI]s(?=[A-Z0-1_]|$)|[hH]as(?=[A-Z0-1_]|$)))?_?(?<target>(?<=\w)\w*)|[A-z_]\w*)?$"
test_str = ("/Class and Namespace Capture\n"
"Nam_e\n"
"\\_Nam_e\n"
"Nam_e\\Spac_e\n\n"
"/Class Method and Namespace Capture\n"
"/With Operation and Target\n"
"Nam_e::set_Singular\n"
"\\Nam_e\\Spac_e::isSingular\n"
"\\Nam_e\\Spac_e::\n\n"
"/Class Method and Deep Namespace Capture\n"
"/Without Operation and Target\n"
"Nam_e\\Spac_e\\Accessible\\Caller\\Value::setsingular\n"
"\\Nam_e\\Spac_e\\Accessible\\Caller\\Value::singular\n\n"
"/Class Method and Deep Namespace Capture\n"
"/With Mutator Operation and Target\n"
"Nam_e\\Spac_e\\Accessible\\Caller\\Value::setSingular\n\n"
"/Class Method and Deep Namespace Capture\n"
"/With Accessor Operation and Target\n"
"Nam_e\\Spac_e\\Accessible\\Caller\\Value::get_singular\n\n"
"/Class Method and Deep Namespace Capture\n"
"/With Conditional Operation and Target\n"
"\\Nam_e\\Spac_e\\Accessible\\Caller\\Value::isSingular\n"
"Nam_e\\Spac_e\\Accessible\\Caller\\Value::has_Singular")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html