UltraEdit or Unix regular expression syntax are easy to learn as all you need to know is the small set of special characters as described in help of UltraEdit and listed when clicking on the button with the triangle in the Find/Replace window and UltraEdit or Unix regular expression option is enabled.
But Perl regular expression engine is much, much more powerful than the other two and much older regular expression engines. So you might start learning the Perl regexp syntax. But be aware of the fact that the Perl syntax explanation fills entire books or complete websites and not just a single small table. Nevertheless it is worth to learn the syntax when you need to reformat often text files.pietzcker
posted at How to delete all lines NOT containing specific word or string or expression?
the expression used here and posted also from which webpage he has this regular expression.
The dot means in Perl (and Unix) syntax simple any character except carriage return and line-feed. I must add "usually any character except new line characters" because it is possible with an addition at beginning of a search string that the dot matches also new line characters, see "." in Perl regular expressions doesn't include CRLFs?
The combination .*
means 0 or more characters except new line characters. That there is a closing parenthesis between these 2 special characters is necessary for the negative lookahead.
This expression is definitely an expert expression and very hard to understand for a beginner.^
means start search at beginning of a line.(?:
is a non marking (non tagging) group. The *\r\n
after the closing parenthesis of this group means that the expression inside the group must be positively applied 0 or more times up to a carriage return and line-feed pair. If the expression inside the non marking group fails anywhere within the line, the find result is negative for this line.(?!
is a negative lookahead expression. Lookahead is like lookbehind a special feature of Perl regular expression engine which are supported all by other regular expression engines using also Perl syntax. The RegExp
class of Qt supports lookahead, but not lookbehind. The .NET RegEx
class supports lookahead and lookbehind. In this search string the negative lookahead is applied on any character of the line because of the dot.
The expression simply means
- begin search at beginning of a line,
- find any character except new line characters,
- verify that this character and the other characters to right are not matching the string in the negative lookahead expression,
- if the actual character and the characters to right match the string in the negative lookahed, the find is negative for this line and continue find at step 1 except last line of file is already reached,
- otherwise continue on step 2 if there is one more character not being a carriage return or line-feed.
- Finally if carriage return and line-feed is found, the result of the find is positive and therefore apply the replace string on found string (=entire line).
- After replace continue at step 1 except end of file is reached.