REGEX Find/Replace Syntax Basics - CSV Files

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

REGEX Find/Replace Syntax Basics - CSV Files

Postby bp83 » Thu Jan 03, 2013 12:17 pm

I am a non-programmer newbie using UE 15.10 UltraEdit Regular Expression Engine in DOS mode to edit large csv files. In reading through the help, tutorials and forum I am feeling good about what I have learned about several of UE's regex find expressions but I am still struggling to understand the meaning of some of the basic replace with syntax. Find "^([~"^p]^)^p" and replace with "^1 " works for the CRLF issue and some basic text string find/replace I found in the UE tutorials/help work great. However, I need to understand what the "1" in the "^1" replace with expression represents and what other functions are available so I can figure out all of the correct replace with syntaxs to use with other common find expressions in csv files such as find "^([~"^r]^)^r" that correctly removes the offending character but does not remove any of the found data. Is there some sort of a syntax overview or additional comprehensive list of common find/replace expressions to use when cleaning up csv files? Any help is greatly appreciated.

Example before:
"123456","ABCDEF","451","A","*** File Documentation ***
file: 123456
I"

Desired after:
"123456","ABCDEF","451","A","*** File Documentation *** file: 123456 I"
bp83
Newbie
 
Posts: 4
Joined: Mon Nov 26, 2012 11:35 am

Re: REGEX Find/Replace Syntax Basics - CSV Files

Postby Mofi » Fri Jan 04, 2013 6:18 am

If you click in Find or Replace dialog on button Help or press key F1, the appropriate help page opens. The help pages for the Find and Replace dialogs contain both the links to

  • Regular Expressions help page listing and explaining briefly in two tables all special characters of UltraEdit and Unix regular expression engine. On this page you can read also about the meaning of ^(...^) in search string and the corresponding reference ^1 in replace string. A bit better explained is that expression on power tip page about tagged regular expressions.
  • Perl Regular Expressions help page explaining and listing the commonly used Perl expressions. This page does not list all possibilities and expressions which can be used with the Perl regexp engine as this fills books or entire websites as those listed on forum announcement Readme for the Find/Replace/Regular Expressions forum.

Explanation for search string "^([~"^p]^)^p" and replace string "^1 ":

With ^(...^) the string found by the expression inside the escaped parentheses is remembered internally for every replace in a string variable. Up to 9 such tagging or marking groups can be used within a search string. The contents of the up to 9 string variables holding parts of a found string are referenced by ^1 to ^9. Such a tagged expression is used if a part of a found string should be kept unchanged in replace or moved to another position within entire found string. In comparison to the UltraEdit regexp engine just (...) must be used for defining a tagging group with Unix/Perl regexp engine and if round brackets should be found, they must be escaped with a backlash in Unix/Perl syntax.

[...] defines a character class, i.e. a set of characters. For example ["3^t] means that either a double quote charater OR character 3 OR a tab character must be found. With ~ immediately after opening square bracket the character class definition becomes a negative definition. So [~"^p] means find a character which is whether a double quote character, NOR a carriage return (CR), NOR a line-feed (LF).

^p is replaced internally by ^r^n wherever used in an UltraEdit regular expression search or replace string.

So the entire search string "^([~"^p]^)^p" means:

  • Find a single character which is whether a double quote nor a CR nor a LF and remember this character in the string variable 1,
  • and next 2 characters are a CR+LF pair
and replace the found string by the found character before CR+LF and a space character instead of the CR+LF pair.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: REGEX Find/Replace Syntax Basics - CSV Files

Postby bp83 » Fri Jan 04, 2013 12:13 pm

Mofi - Thank you very much for your patience and the thorough explanation. It sounds like I need to dive into the tagged regular expressions to ensure that I fully grasp how they work. Thanks again.
bp83
Newbie
 
Posts: 4
Joined: Mon Nov 26, 2012 11:35 am


Return to Find/Replace/Regular Expressions