Finally learning REGEX, but need help

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

Finally learning REGEX, but need help

Postby lm77054 » Thu Dec 27, 2012 10:57 am

Hey all:

Yesterday I started to get into REGEX, because I started a project that is forcing me to. I read and played with REGEX all last evening, and I'm still having difficulty with the main items I need to use REGEX with. While in ULTRAEDIT, I tried the three main REGEX expressions I need, and I can't get them right. Let's say that you have the phrase:

The quick brown fox jumps over the lazy dogs back.

Here is what I'm trying to do:

1. If the item I'm comparing has "The " (the space is significant) at the beginning, remove it. But I don't want the second "the" messed with.
2. If the item I'm comparing has "back." at the end, remove it.
3. If the item I'm comparing has "over" in it, change it to "under".
4. Finally, if the item I'm comparing has "lazy", remove it completely.

I'm still trying to learn REGEX. After what "Mofi" helped me with before (thanks again, "Mofi"), it peeked my interest in it, but I was never forced into learning it; until now. I've gone through this forum, and I couldn't locate things similiar to this; hopefully someone can help.

For any help that can be given I will say THANK YOU, THANK YOU in advance!

I forgot to say, that I would be using a script that would be reading one line at a time and doing the comparisons, and only of for instance condition #1 applied, then make the change.
lm77054
Basic User
Basic User
 
Posts: 15
Joined: Sat Feb 26, 2011 12:11 pm

Re: Finally learning REGEX, but need help

Postby spaceBAR » Thu Dec 27, 2012 3:13 pm

I like to use the "Perl" regular expressions engine, These are just examples of one way to do what you want to do:
1. If the item I'm comparing has "The " (the space is significant) at the beginning, remove it. But I don't want the second "the" messed with.

In the "Find what" window put "^The " without the quotes and make sure the "Replace with" window contains nothing and click 'Replace All'.

2. If the item I'm comparing has "back." at the end, remove it.

In the "Find what" window put "back\.$" without the quotes and make sure the "Replace with" window contains nothing and click 'Replace All'.

3. If the item I'm comparing has "over" in it, change it to "under".

In the "Find what" window put "over" without the quotes and put "under" without the quotes in the "Replace with" window and click 'Replace All'.

4. Finally, if the item I'm comparing has "lazy", remove it completely.

In the "Find what" window put "lazy" without the quotes and make sure the "Replace with" window contains nothing and click 'Replace All'.

Just a few Perl Regular Expressions Tutorials:
http://www.enginsite.com/Library-Perl-Regular-Expressions-Tutorial.htm
http://perldoc.perl.org/perlretut.html
http://www.comp.leeds.ac.uk/Perl/matching.html

Online Perl regular expressions testers:
http://retester-daoswald.dotcloud.com/
http://www.perlfect.com/articles/regextutor.shtml
User avatar
spaceBAR
Basic User
Basic User
 
Posts: 20
Joined: Tue Oct 19, 2004 11:00 pm

Re: Finally learning REGEX, but need help

Postby Mofi » Fri Dec 28, 2012 5:01 am

What spacebar wrote is correct for what you wrote in your 4 points. But it looks like you want to run the replaces from within a script and with item in point 4 you mean an entire line.

Code: Select all
if (UltraEdit.document.length > 0) {

   // Define environment for script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Make sure that file ends with a line termination.
   UltraEdit.activeDocument.bottom();
   if (UltraEdit.activeDocument.isColNumGt(1)) {
      UltraEdit.activeDocument.insertLine();
      if (UltraEdit.activeDocument.isColNumGt(1)) {
         UltraEdit.activeDocument.deleteToStartOfLine();
      }
   }
   UltraEdit.activeDocument.top();

   // Define all parameters for the Perl regular expression replaces.
   UltraEdit.perlReOn();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   UltraEdit.activeDocument.findReplace.replace("^The ", "");
   UltraEdit.activeDocument.findReplace.replace("back\\.$", "");
   UltraEdit.activeDocument.findReplace.replace("^.*lazy.*\\r\\n", "");
   UltraEdit.activeDocument.findReplace.regExp=false;
   UltraEdit.activeDocument.findReplace.replace("over", "under");
}

Please note that in Javascript strings the backslash character is the escape character. So to pass a backslash character to the Perl regular expression engine of UltraEdit you have to escape every backslash in a search or replace string with one more backslash character.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions

cron