# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
(?<Loop>
(?<NewBlock>
(?:(?:
^[\s]*\n
|
^(?:(?!\/\*).)+\n
|
^(?:(?!\/\*).)+
))+
)?
(?<OldBlock>
(?:
\/\*.+\*\/
|
(?:\/\*.*\n)
(?:(?:
^[\s]*\n
|
^(?:(?!\*\/).)+\n
|
^(?:(?!\*\/).)+
))+
\*\/
)
)?
)+
"""
test_str = (" .highlight\n"
" {\n"
" background: #ffffff\n"
" }\n\n"
" .highlight .c,\n"
" .highlight .cm,\n"
" .highlight .cp,\n"
" .highlight .c1,\n"
" .highlight .cs\n"
" {\n"
" color: #007400;\n"
" }\n"
" /* Comment, \n"
" Comment.Multiline, \n"
" Comment.Preproc, \n"
" Comment.Single, \n"
" Comment.Special\n"
" */\n\n"
" .highlight .k,\n"
" .highlight .kc,\n"
" .highlight .kd,\n"
" .highlight .kp,\n"
" .highlight .kr,\n"
" .highlight .nb\n"
" {\n"
" color: #AA0D91;\n"
" }\n"
" /* Keyword,\n"
" Keyword.Constant,\n"
" Keyword.Declaration,\n"
" Keyword.Pseudo,\n"
" Name.Builtin */\n\n"
" .highlight .kt\n"
" {\n"
" color: #3F6E74\n"
" }\n\n"
" /* Keyword.Reserved */\n"
" .highlight .err\n"
" {\n"
" color: #a61717;\n"
" }\n\n"
" /* Error */\n"
" .highlight .o\n"
" {\n"
" font-weight: bold\n"
" }\n\n"
" /* Operator */\n"
" .highlight .gd\n"
" {\n"
" color: #000000;\n"
" background-color: #fdd\n"
" }\n\n"
" /* Generic.Deleted */\n"
" .highlight .gd .x\n"
" {\n"
" color: #000000;\n"
" background-color: #faa\n"
" }\n\n"
" /* Generic.Deleted.Specific */\n"
" .highlight .ge\n"
" {\n"
" font-style: italic\n"
" }\n\n"
" /* Generic.Emph */\n"
" .highlight .gr\n"
" {\n"
" color: #a00\n"
" }\n\n"
" /* Generic.Error */\n"
" .highlight .gh\n"
" {\n"
" color: #999\n"
" }\n\n"
" /* Generic.Heading */\n"
" .highlight .gi\n"
" {\n"
" color: #000000;\n"
" background-color: #dfd\n"
" }\n\n"
" /* Generic.Inserted */\n"
" .highlight .gi .x\n"
" {\n"
" color: #000000;\n"
" background-color: #afa\n"
" }\n\n"
" /* Generic.Inserted.Specific */\n"
" .highlight .go\n"
" {\n"
" color: #888\n"
" }\n\n"
" /* Generic.Output */\n"
" .highlight .gp\n"
" {\n"
" color: #555\n"
" }\n\n"
" /* Generic.Prompt */\n"
" .highlight .gs\n"
" {\n"
" font-weight: bold\n"
" }\n\n"
" /* Generic.Strong */\n"
" .highlight .gu\n"
" {\n"
" color: #aaa\n"
" }\n\n"
" /* Generic.Subheading */\n"
" .highlight .gt\n"
" {\n"
" color: #a00\n"
" }\n\n"
" /* Generic.Traceback */\n"
" .highlight .m,\n"
" .highlight .mf,\n"
" .highlight .mh,\n"
" .highlight .mi,\n"
" .highlight .mo\n"
" {\n"
" color: #1C00CF;\n"
" }\n\n"
" /* Literal.Number,\n"
" Literal.Number.Float,\n"
" Literal.Number.Hex,\n"
" Literal.Number.Integer,\n"
" Literal.Number.Oct */\n"
" .highlight .s\n"
" {\n"
" color: #C41A16;\n"
" }\n\n"
" /* Literal.String */\n"
" .highlight .na\n"
" {\n"
" color: teal\n"
" }\n\n"
" /* Name.Attribute */\n"
" .highlight .nc\n"
" {\n"
" color: #5C2699\n"
" }\n\n"
" /* Name.Class */\n"
" .highlight .no\n"
" {\n"
" color: teal\n"
" }\n\n"
" /* Name.Constant */\n"
" .highlight .ni\n"
" {\n"
" color: purple\n"
" }\n\n"
" /* Name.Entity */\n"
" .highlight .ne\n"
" {\n"
" color: #990000\n"
" }\n\n"
" /* Name.Exception */\n"
" .highlight .nn\n"
" {\n"
" color: #555\n"
" }\n\n"
" /* Name.Namespace */\n"
" .highlight .nt\n"
" {\n"
" color: navy\n"
" }\n\n"
" /* Name.Tag */\n"
" .highlight .nv,\n"
" .highlight .nf\n"
" {\n"
" color: #3F6E74\n"
" }\n\n"
" /* Name.Variable,\n"
" Name.Function */\n"
" .highlight .ow\n"
" {\n"
" font-weight: bold\n"
" }\n\n"
" /* Operator.Word */\n"
" .highlight .w\n"
" {\n"
" color: #bbb\n"
" }\n\n"
" /* Text.Whitespace */\n"
" .highlight .sb\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Backtick */\n"
" .highlight .sc\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Char */\n"
" .highlight .sd\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Doc */\n"
" .highlight .s2\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Double */\n"
" .highlight .se\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Escape */\n"
" .highlight .sh\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Heredoc */\n"
" .highlight .si\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Interpol */\n"
" .highlight .sx\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Other */\n"
" .highlight .sr\n"
" {\n"
" color: #009926\n"
" }\n\n"
" /* Literal.String.Regex */\n"
" .highlight .s1\n"
" {\n"
" color: #d14\n"
" }\n\n"
" /* Literal.String.Single */\n"
" .highlight .ss\n"
" {\n"
" color: #990073\n"
" }\n\n"
" /* Literal.String.Symbol */\n"
" .highlight .bp\n"
" {\n"
" color: #999\n"
" }\n\n"
" /* Name.Builtin.Pseudo */\n"
" .highlight .vc\n"
" {\n"
" color: teal\n"
" }\n\n"
" /* Name.Variable.Class */\n"
" .highlight .vg\n"
" {\n"
" color: teal\n"
" }\n\n"
" /* Name.Variable.Global */\n"
" .highlight .vi\n"
" {\n"
" color: teal\n"
" }\n\n"
" /* Name.Variable.Instance */\n"
" .highlight .il\n"
" {\n"
" color: #099\n"
" }\n\n"
" /* Literal.Number.Integer.Long */\n"
" *,*:before,*:after\n"
" {\n"
" -moz-box-sizing: border-box;\n"
" -webkit-box-sizing: border-box;\n"
" box-sizing: border-box")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
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