Regular Expressions

swazz99jhb

Well-Known Member
Joined
Nov 20, 2005
Messages
229
Reaction score
6
Location
Johannesburg
Hi

I need to Identify and replace a string using RegEx.

So anywhere in the CONTENT I need to find the string "comment" then find the matching < and > sign and replace everything including the signs with spaces.

String is
Code:
<a>sample</a><img src="http://icon_comment.gif" alt="This post has 1 comments." title="This post has 1 comments."/><a rel="nofollow" target="_blank" href="http://examples.org/user/example">

Changed to:
Code:
</a><a rel="nofollow" target="_blank" href="http://examples.org/user/example">


Any ideas?

Thanks
 
Last edited:
Try this

<.*?comment.*?>

Not sure which language you are using so can't give you the syntax for more than the RegEx.
 
I need to Identify and replace a string using RegEx.

So anywhere in the CONTENT I need to find the string "comment" then find the matching < and > sign and replace everything including the signs with spaces.

For me

Code:
sed -r 's:<img.+?comment.+?/>::g' oldfile.txt > newfile.txt

worked just dandy. It might not work if there is a colon ( : ) in the text to be matched but you can replace the colon by another suitable character. sed is your friend.
 
Last edited:
For me

Code:
sed -r 's:<img.+?comment.+?/>::g' oldfile.txt > newfile.txt

worked just dandy. It might not work if there is a colon ( : ) in the text to be matched but you can replace the colon by another suitable character. sed is your friend.

Thanks, but won't work for me.

1st problem: Assume sed is a linux command. So won't work for me.
2nd problem: the <img doesn't seem to find the 1st occurrence of <img before the comment, but rather the 1st occurrence of <img in the entire CONTENT string.
 
Thanks, but no luck. Using a expression tester tool on 215design dot com to test.

Its great your asking questions on the forum, but do you actually know anything about regular expressions? If not then first make an effort to learn the stuff.

The expression your looking for is rather basic but you also need to assist us with what your using on your side as from experience, I can say not all regex apps/functions are equal.
 
Its great your asking questions on the forum, but do you actually know anything about regular expressions? If not then first make an effort to learn the stuff.

The expression your looking for is rather basic but you also need to assist us with what your using on your side as from experience, I can say not all regex apps/functions are equal.

Started using it today, so won't call myself an expert, but think I understand the just of it. I've been struggling with this since 11:00 this morning so thought I'll ask. Clearly I'm missing something.
 
From Scott Hanselman's Ultimate Tools...

Regular Expressions

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski

TextCrawler - I used to use Funduc's Search and Replace for multi-file search and replace with regular expressions, but somehow the interface of TextCrawler is more intuitive to me.
David Seruyange's "NRegEx" Ajax-based RegEx Tester - An very minimalist online Ajax-based ASP.NET site, I keep turning to this via a bookmark when I want to test a quick RegEx. It'll tell me how a RegEx will work in .NET.
gSkinner - An amazing Flash-based online RegEx tool for writing and testing RegEx.
RexV - Another excellent, better laid out RegEx evaluator, useful for RegEx's that'll run in JavaScript.
Expresso - Almost 8 years old and extremely mature, Expresso is now free!
Roy Osherove's Regulator - Roy entered the RegEx fray with a bang, and with syntax highlighting and web services integration with regexlib.com. The very definition of slick.
Regulazy - Currently at version 1.01, this tool is a great way for newbies to start using Regular Expressions. Write regular expressions without prior knowledge of the syntax!
Collection of Regular Expressions Toolbox - When I'm overwhelmed, I start here. A huge list of all the basics of Regular Expressions, laid out cleanly and logically. I also like these 8 Regular Expressions You Should Know.
RegexDesigner.NET from Chris Sells - Simple, elegant, small. A great little application. Almost 10 years old, and still useful.
Find the links at Scott Hanselman's 2011 Ultimate Developer and Power Users Tool List for Windows
 
Top
Sign up to the MyBroadband newsletter
X