Avoiding Find matches within quotes

Help with writing and playing macros

Avoiding Find matches within quotes

Postby epoclaen » Wed Sep 02, 2009 11:28 pm

Hey all,
I'm trying to work out a regular expression for a search (and replace) that will only locate an exclamation point if it lies outside of any open and close double quote sections on the line the cursor begins its positioning on. I want the cursor to end up in front of the last exclamation point in the line that doesn't reside between a set of quotes. In other words here's what would and wouldn't be a match:

string1="This is a string!" //no match
string2=("This string is " + "concatenated!") //no match
if ("This new string" != "The old string") then //matches
string3="Here's yet another string." !! And a comment about it. //matches


What I've been trying is several modifications of the following to find just exclamation points on the line with no quotes after them at all:

UnixReOn
Key HOME
IfColNumGt 1
Key HOME
Find RegExp Select "![^"]+$"

but this likely isn't the way to go and has been selecting the entire line anyway. It's almost like I'll have to strip the line of any open/close quote sections and then search for any remaining exclamation points but I hate to have to resort to opening a new file just for string manipulation since this macro will likely be tied to a larger macro that will search the entire file.

Any ideas?

Thanks,
Jeff
epoclaen
Newbie
 
Posts: 3
Joined: Wed Sep 02, 2009 10:17 pm

Re: Avoiding Find matches within quotes

Postby Mofi » Thu Sep 03, 2009 1:28 am

If this is possible then only with a very complex Perl regular expression which supports lookaround. You wrote that you want to use this part in a larger macro working on the entire file. Here is my solution. First search for escaped double quote characters and replace it with a special string. Second search in a loop for single line strings which are selected when found and replace all ! in the selected strings by a special string. After this part of the macro only ! exist outside of strings which should it make easier for you to do what you want with the macro. Finally the special strings used as placeholders are replaced by the original strings.

The macro property Continue if search string not found must be checked for this macro.

InsertMode
ColumnModeOff
HexOff
UnixReOn
Top
Find MatchCase "\""
Replace All "DoUbLeQuOtE"
Loop
Find MatchCase RegExp "".*""
IfNotFound
ExitLoop
Else
Find MatchCase "!"
Replace All SelectText "ExClAmAtIoN"
EndIf
EndLoop
Top
further commands
Top
Find MatchCase "DoUbLeQuOtE"
Replace All "\""
Find MatchCase "ExClAmAtIoN"
Replace All "!"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Avoiding Find matches within quotes

Postby epoclaen » Thu Sep 03, 2009 9:03 am

Thanks Mofi!
This works well in my tests but could you answer some questions for me so I understand what's happening?

InsertMode
ColumnModeOff
HexOff
UnixReOn //Using Unix Regular Expressions rather than the UE RegEx.
Top
Find MatchCase "\"" // Find "escaped" double quotes? None of my quotes
Replace All "DoUbLeQuOtE" // were replaced so I assume this is just for some potentially oddly ASCii coded quotes?
Loop
Find MatchCase RegExp "".*"" // Find any open+closing quote blocks of code that lie on the same line.
IfNotFound
ExitLoop
Else // If found then...
Find MatchCase "!" // Ummm... I don't see in the help files for version 13.20a where
// the search will be limited only to selected text (if any is
// selected). Is this true? I have to assume that it is.

Replace All SelectText "ExClAmAtIoN" // Replace the exclamation point.
EndIf
EndLoop // Look for the next set of quotes on a single line.
Top
further commands
Top
Find MatchCase "DoUblEqUoTe"
Replace All "\""
Find MatchCase "ExClAmAtIoN"
Replace All "!"
epoclaen
Newbie
 
Posts: 3
Joined: Wed Sep 02, 2009 10:17 pm

Re: Avoiding Find matches within quotes

Postby Mofi » Thu Sep 03, 2009 11:14 am

The replace of all occurrences of \" is for something like "File \"%s\" not found!"
Such an expression must be used in C, C++, JScript (Javascript), etc. to specify a double quote character inside a double quoted string. I don't know in which language your code is and how a double quote character in this language must be coded in the strings. If for example a double quote character must be coded with an additional preceding double quote character like in CSV files, you can delete the 2 Find+Replace commands to replace all \" to DoUbLeQuOtE and back.


Find MatchCase "!"
Replace All SelectText "ExClAmAtIoN"

Your version of UltraEdit does not support find in selected text like UltraEdit v14.00 and later do. But that is not important here. Find+Replace are just on 2 lines in the macro editor for supporting multi-line search and replace strings. In real this is just one command, the replace command. If you open the replace dialog with Ctrl+R and look on the options you see that you can choose in the Find Where box the option Selected Text. In your version of UltraEdit this option is available only for the replace command which is the reason why the parameter SelectText (better would be the name SelectedText) must be specified on the replace command line. Both together simply means: replace all occurrences of ! by ExClAmAtIoN in currently selected text only.

And the entire string was selected before with the Unix regular expression search for ".*" which means find a byte sequence starting with a double quote character, followed by 0 or more occurrences of any character except new line characters until another double quote character is found. Well, search string ".+" would be better to ignore empty strings "" which surely do not contain an exclamation mark.

By the way: I just thought about possible problems with this macro and there is one. A single double quote character in a line which contains right the single double quote character also a string in double quotes could result in a wrong behavior if the single double quote character has no escape character. For example something like:

var sText = '\"' + " !!! test !!! " + '\"';

would be no problem. But if you can write in your language:

string2=('"' + " !!! test !!! " + '"')

you would need an additional replace all command at top of the macro to replace all '"' by 'DoUbLeQuOtE'.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4054
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Avoiding Find matches within quotes

Postby epoclaen » Thu Sep 03, 2009 12:28 pm

Makes much more sense now. Thanks again Mofi!

And yeah, my scripting language is protected by an NDA so sorry I couldn't be clearer. There's no escape characters or other instances that might upset the search so I think I'm good.
epoclaen
Newbie
 
Posts: 3
Joined: Wed Sep 02, 2009 10:17 pm


Return to Macros