3 ms·
To write regex with relative ease does require mastering some basic fundamentals of Regex. For example to have a regex that can remove // from inline comment o
by egberts1 4y ago
To write regex with relative ease does require mastering some basic fundamentals of Regex.
For example to have a regex that can remove // from inline comment of an INI-format text file.
(((\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\|\~]*))*)*)([;#]+)*.*$
That regex works very well and it’s breakdown can be better explained using various online Regex testers
Off-topically, I’ve got another next-level problem, and that is how to convert Regex for use within a bash shell script.
It’s nasty.
However, I have listed several online Regex testers so you can better understand the result of (my) Regex creation, one regex character at a time.
https://www.debuggex.com/ https://www.debuggex.com/
https://extendsclass.com/regex/4d49f33 https://extendsclass.com/regex/4d49f33
https://www.freeformatter.com/regex-tester.html https://www.freeformatter.com/regex-tester.html
https://regexr.com/6gv39 https://regexr.com/6gv39
More online testers that I couldn’t get to work with are detailed in https://github.com/egberts/bash-ini-file/issues/1 https://github.com/egberts/bash-ini-file/issues/1
- Veserv 4y agoA 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.
- _8j50 4y agoSad to see regex101.com left out in your list.
- egberts1 4y agoI now just remember going to regex101 and struggling to get my long pattern working back in the days. And now I’ve neglected to mention regex101.com as totally working just perfectly (for I’ve just tested them as WORKING WELL just now). I do still plan to them. Thanks for the heads up!