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