Regular Expression Puzzle

Ray Parrish crp at cmc.net
Sun Nov 29 11:53:55 UTC 2009


Hello,

I am trying to fathom a way to include a regular expression in a Search 
Term, and a Replace Term so that I can replace text strings that vary 
somewhat. For example, I have the following two lines in Test.html

<div><a href="index.html">Ray's Links Home</a> / <a 
href="Science.html">Science</a> / Biology</div>
<div><a href="index.html">Ray's Links Home</a> / <a href="Web 
Publishing.html">Web Publishing</a> / Web Editors</div>

I would like to replace the <div>, and </div> tags with <p></p> for 
instance. What I did is -

ray at RaysComputer:~/test$ FileContents=`cat Test.html`
ray at RaysComputer:~/test$ SearchTerm="<div><a href=\"index.html\">Ray's 
Links Home</a>*</div>"
ray at RaysComputer:~/test$ ReplaceTerm="<p><a href=\"index.html\">Ray's 
Links Home</a>*</p>"
ray at RaysComputer:~/test$ 
FileContents=${FileContents/$SearchTerm/$ReplaceTerm}
ray at RaysComputer:~/test$ echo "$FileContents"
<p><a href="index.html">Ray's Links Home</a>*</p>
ray at RaysComputer:~/test$

As you can see, it did not turn out quite how i wanted it to. I want to 
preserve everything between the <div></div> tags, and replace them with 
paragraph tags for this experiment.

I thought the * in the SearchTerm, and ReplaceTerm would act as a 
wildcard, and preserve what was there, but it replaces it all with itself.

Can anybody give me some guidance here?

Thanks for any help you can be, Ray Parrish

-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list