Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Regular Expressions in @REREPLACE

Jun
562
4
Am I misunderstanding something here. The first command does what I expect. In the second one, the negation of the character set is ignored.

TCC(30.00.22): C:\>echo %@rereplace["h[io]",!,hat het hit hot hut]
hat het !t !t hut

TCC(30.00.22): C:\>echo %@rereplace["h[^io]",!,hat het hit hot hut]
hat het !t !t hut

When I check this with the built-in regular expression checker, the expressions work correctly.
 
When I check this with the built-in regular expression checker, the expressions work correctly.
That might be because it's not being parsed like a command line. It does work if you double the '^'. I reckon a single '^' is getting parsed out of existence. The double quotes would normally protect it so i'm not sure what's going on.

v:\> echo %@rereplace["h[^^io]",!,hat het hit hot hut]
!t !t hit hot !t
 
Thank you, Vince. I tried all kinds of things to protect that caret, and nothing worked. Initially, I had no surrounding quotes, since the help example did not have them. I hoped that quoted the regular expression would solve the problem. All this is probably obvious to Rex, since he knows the details of the order in which all the parsing is done.

What I was trying ultimately to do was find doubled quotation marks in a CSV file that are not followed by a comma (i.e., are not empty fields but around data internal to a field) and to replace the doubled quotation marks with the HTML entity ". Quotation marks get doubled in a CSV file field that is surrounded by quotation marks (e. g., because the field contains a comma). However, empty fields also end up with a double comma in the CSV file produced by the web form I'm using (which quotes all fields). That turned out to require more tricky escapes.

The character class [^^,] would not work for detecting a non-comma. The form [^^^c] works! Then one has to deal with the ampersand in the HTML entity.

TCC(30.00.22): C:\>echo %@rereplace[\"\"([^^^c]),^"\1,this is a ""quoted string"" string,"","another field"]
this is a "quoted string" string,"","another field"

Oops. I'm not done yet. The string with the quoted text is surrounded by single quotes, and the example above does not work.

TCC(30.00.22): C:\>echo %@rereplace[\"\"([^^^,]),^"\1,"A ""quoted string"" in field0","","field2"]
"A ^"quoted string^" in field0","","field2"

I don't understand this yet. Now I don't need to (and must not) escape the ampersand in the replacement string. When I didn't quote it before, it would be interpreted as a command separator (I think) and throw an error message.

TCC(30.00.22): C:\>echo %@rereplace[\"\"([^^^,]),"\1,"A ""quoted string"" in field0","","field2"]
"A "quoted string" in field0","","field2"

Now I'll see if this works in my real code.
 

Similar threads

Back
Top