3 ms·
A regular expression like that is hard to decipher because it is a giant pile of unformatted duplication. It is challenging to read in the same way that a progr
by Veserv 4y ago
A regular expression like that is hard to decipher because it is a giant pile of unformatted duplication. It is challenging to read in the same way that a program written by manually inlining every function call in the entire program and then putting everything onto the same line (because you have a non-newline statement separator) is hard to read.
Let us see how to use the advanced technology of factoring out common expressions into "subroutines" makes it much easier to understand. Note, I have no idea of what the regex you are doing actually does, so I am doing things purely mechanically, and I have only basic working-memory knowledge of regexes.
First, let us restructure it:
(
(
(
\x27
[ \!\"\#\$\%\&\(\)\*\+\-\.\/0-9\:\;\<\=\>\?@A-Z\[\\\]\^\_\`a-z\|\~]*
\x27\s*
)
|(
\"
[ \!\#\$\%\&\x27\(\)\*\+\-\.\/0-9\:\;\<\=\>\?@A-Z\[\\\]\^\_\`a-z\|\~]*
\"\s*
)
|(
\/(
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]+
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*
)
|(
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*
)
)*
)*
)
([;#]+)*.*$
Now let us reorganize the clearly common expressions:
(
(
(
\x27
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~\;\#\/\\\"]*
\x27\s*
)
|(
\"
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~\;\#\/\\\x27]*
\"\s*
)
|(
\/(
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]+
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*
)
|(
[ \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~]*
)
)*
)*
)
([;#]+)*.*$
Now let us factor out the common expressions:
common = { \!\$\%\&\(\)\*\+\-\.0-9\:\<\=\>\?@A-Z\[\]\^\_\`a-z\|\~}
separator = {\;\#\/\\}
(
(
(
\x27
[common + separator + {\"}]*
\x27\s*
)
|(
\"
[common + separator + {\x27}]*
\"\s*
)
|(
\/(
[common]+[common]*
)
|(
[common]*
)
)*
)*
)
([;#]+)*.*$
At this point, going any further likely requires a deeper understanding of what it is actually trying to accomplish, but the purely mechanical transformations have dramatically reduced complexity of the regex to something in the realm of comprehensibility.
- labrador 4y agoExactly. I would never write a long run on regex like parent. I'd break it down like this so I could read it when I came back to it later. Nice job.
- egberts1 4y agoSome regex engines cannot handle separate parsing (due to need of a state machine to track next continued line) broken down due to [prior] end-of-line notation. and most regex engines cannot handling labels of assignments for later reuse within its expression. It is however still useful to leverage a higher language to do the constructing of a final regex pattern from repeated patterns (basic building block) prior to engine usages.
- labrador 4y agoI get around it by using specifiers in strings and using string substitution before passing off to the Regex
- egberts1 4y agoThat’s a great breakdown. Some online regex testers do do that common grouping as well. The problem statement can be described as something like: how to remove trailing inline comment after properly skipping over any matching pair of quotes, single or double, that are found before the inline comment notation, whose removal is starting with the comment notation ‘#’ or ‘//‘ on each non-continued line within a .ini text file.