How to replace patterns and preserve some variable portions?

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

How to replace patterns and preserve some variable portions?

Postby elim » Wed Mar 22, 2006 2:03 pm

Sorry I don't even know how to write the subject of my question. And thus don't know how to search possible existing solutions. But here is the case:

I've many html files in a folder and they contain the pattern:

<a href="../ch[V1]/[V2].html">

where [V1] and [V2] varies among the files:
<a href="../ch01/01-03.html">
.....
<a href="../ch12/8.html">
.....

I want to change them into the pattern:

<a href="[V1]_[V2].html">

Thus
<a href="../ch01/01-03.html"> becomes
<a href="01_01-03.html"> and <a href="../ch12/8.html"> becomes
<a href="12_8.html"> .....

Is there a way to make such changes by a single replace with UE32?

Thanks a lot

elim@ellinkage.net
User avatar
elim
Newbie
 
Posts: 8
Joined: Mon Sep 12, 2005 11:00 pm

Re: How to replace patterns and preserve some variable portions?

Postby roland » Wed Mar 22, 2006 2:45 pm

Using perl regex (new in 12.00)

Code: Select all
Find:     "../ch([^/]+)/([^.]+)\.html"
Replace:  "\1_\2.html"


I am not familiar enough with UE style regex to comment on that method.
User avatar
roland
Basic User
Basic User
 
Posts: 30
Joined: Sun Aug 15, 2004 11:00 pm

Re: How to replace patterns and preserve some variable portions?

Postby Mofi » Wed Mar 22, 2006 2:51 pm

With Replace In Files with a regular expression. My first one is an UltraEdit style regular expression. First look at Configuration - Searching and make sure that Unix style Regular Expressions or Perl compatible Regular Expressions (UE v12) is not enabled.

Then open Replace In Files, enable Regular Expressions and enter following:

Find What: <a href="../ch^(*^)/^(*^).html">
Replace With: <a href="^1_^2.html">

See help of UltraEdit for details about regular expressions and an explanation how this works.

Because it is easy, here is also the Unix style regular expression (not tested):

Find What: <a href="\.\./ch(.*)/(.*)\.html">
Replace With: <a href="\1_\2.html">
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to replace patterns and preserve some variable portions?

Postby elim » Thu Sep 08, 2011 3:01 pm

Thank you very much roland and Mofi. That worked!
User avatar
elim
Newbie
 
Posts: 8
Joined: Mon Sep 12, 2005 11:00 pm


Return to Find/Replace/Regular Expressions