tags:

views:

116

answers:

3

I am trying to do a search and replace using GREP/Regex

Here is what I am searching for

<div align="center" class="orange-arial-11"><b>.+<br>

I want to remove the <b>, <br> tags, and place <h3> tags around what .+ finds.

But I can't get what .+ finds to stay when it does the replace.

For example, I want to find this

<div align="center" class="orange-arial-11"><b>This is the section I want intact<br>

to change to this

<div align="center" class="orange-arial-11"><h3>This is the section I want intact</h3>

Any help is appreciated.

+1  A: 

It depends exactly what system you're using, but if you put something in parenthesis you can refer to it later

So it might be something like

s/<b>(.+)<br>/<h3>\1<\/h3>/
swampsjohn
I am just using the search/replace in textwrangler.
Brad
Ok, then use what I said. Text to find = <b>(.+)<br> Replace with: <h3>\1</h3> If you need to match the stuff at the beginning, do (foo)<b>(.+)<br> and \1<h3>\2</h3>
swampsjohn
+3  A: 

Use sed instead of grep:

# Modify the file in-place
sed -i~ 's|\(<div align="center" class="orange-arial-11">\)<b>\(.\+\)<br>|\1<h3>\2</h3>|' the-file
Adam Rosenfield
sed is not an option
Brad
What about "perl -ple 's/search regex/replace string/"?Grep has absolutely no replace functionality, only searching. Sed is the usual answer.
Mark Santesson
A: 

In TextWrangler:

search for:

<div align="center" class="orange-arial-11"><b>(.+?)<br>

replace with:

<div align="center" class="orange-arial-11"><h3>\1</h3>

The '\1' will be replaced with the string matched inside the parens in the search pattern.

joshng
(Also, using the question mark (.+?) instead of (.+) will ensure that you don't match too much -- otherwise, (.+) might match everything up to the last <br> in the text)
joshng