Ignore line comment lines

Find, replace, find in files, replace in files, regular expressions

Ignore line comment lines

Postby mksql » Fri Sep 08, 2006 6:34 pm

Would like to create a regexp search that ignores lines found that have a line comment marker in them. In my case, I am working in T-SQL, so any line containing a "--" would not be returned. In some cases, the comment marker would not be on the first column,

For example when searching for the string "searchtext" I would want to see the first line, but not the second:

SELECT * FROM searchtext

-- SELECT * FROM searchtext
User avatar
mksql
Newbie
 
Posts: 5
Joined: Wed Dec 01, 2004 12:00 am

Re: Ignore line comment lines

Postby Mofi » Wed Sep 13, 2006 12:12 pm

I don't know how a single regular expression can solve this. Maybe some Perl experts have an idea. My solution uses a macro to find the string and additionally tests, if it is not found on the right side of a line comment. The macro property Continue if a Find with Replace not found must be checked for this macro.

InsertMode
ColumnModeOff
HexOff
UnixReOff
Loop
Find RegExp "%*searchtext"
IfNotFound
ExitLoop
EndIf
Find "--"
Replace All SelectText "--"
IfNotFound
EndSelect
Key HOME
Find RegExp "searchtext"
ExitLoop
Else
EndSelect
Key LEFT ARROW
Key RIGHT ARROW
EndIf
EndLoop

Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.

A more general solution would be to run the 2 finds with the current clipboard content. This could be realized by replacing searchtext with ^c.

Attention: The first find is an UltraEdit style regular expression search and so the text in the clipboard is also interpreted as regular expression. So the clipboard content should not contain any UltraEdit style regular expression character or they are all escaped with the ^ character - see also my small tutorial for ^s and ^c.

If you want to use the macro also with search strings with escaped regular expression characters insert the red marked RegExp option also on the second find, although it is not needed for normal search strings.

For similar examples see:
Delete all lines not starting with a special phrase
Delete only 1st word A in front of word B
How to delete all lines not containing specific word or string or expression?

The following UltraEdit style regular expression will work for all lines where -- is at start of the line or only preceding white-spaces exist before the line comment start and the first 2 non white-space characters are not also the first 2 characters of the search string. It does not work for lines with any non white-space character before the start of the line comment or lines starting with the search string (with or without preceding white-space characters). Maybe this is also enough for you.

Find: %[ ^t]++[~^-^r^n][~^-^r^n]*searchtext
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions