Regular Expression Question

This ought to be easy for you Perl guys (too hardcore for me).

I want to turn this,

[img]<a href="http://www.blah.com/images/blah.gif" target="_blank">http://www.blah.com/images/blah.gif</a>[/img]

into this

<img src="http://www.blah.com/images/blah.gif">

Too much for me.  Is this possible with a regex?  I'm pretty sure it is.  Need a quick answer, so 500 pts.
DzejmsAsked:
Who is Participating?
 
Harisha M GConnect With a Mentor Commented:
Another (preferably final) tweak..

$str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"]?)(.+?)\1[^>]*>.*?</a>\s*\[/img\]#<img href="$2">#ims;
0
 
Harisha M GCommented:
Hi,

$str = q<[img]<a href="http://www.blah.com/images/blah.gif" target="_blank">http://www.blah.com/images/blah.gif</a>[/img]>;

$str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"])(.+?)\1[^>]*>.*?</a>\[/img\]#<img href="$2">#;

print $str;

---
Harish
0
 
Harisha M GCommented:
add the ims options...

$str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"])(.+?)\1[^>]*>.*?</a>\[/img\]#<img href="$2">#ims;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Harisha M GCommented:
A small tweak:

$str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"]?)(.+?)\1[^>]*>.*?</a>\[/img\]#<img href="$2">#ims;
0
 
DzejmsAuthor Commented:
don't know anything about Perl.. just know you guys deal with regex a lot.  I'm actually using this in another programming language.. what's the ims thing do?  Is that Perl specific?
0
 
Harisha M GCommented:
They are modifiers for Ignore case, Multiline etc. and are language specific.

You might also want to use "g" modifier which is for Global (replacement)
0
 
Harisha M GCommented:
.. and in which language are you writing the code ?
0
 
DzejmsAuthor Commented:
ehh.. proprietary software stuff....  Still trying to get it to work... I don't doubt your code, just getting it to fit correctly with different modifiers.  I'll check on it over the weekend.
0
 
Harisha M GCommented:
Nope.. just asked the language of your code.. not the code itself :)
0
 
mjcoyneCommented:
Just to complete the modifiers explanation:

The "s" modifier allows the "match any character" metacharacter (the "." (period), as in ".*") to also match newlines.  So, in the following program, $str is assigned "can you find this<newline>string?".  The first "if" loop will fail to match, but the second "if" loop will match, because I've added the "s" modifier to the regular expression:

#!/usr/bin/perl -w
use strict;

my $str = "can you find this
string?";

if ($str =~ /this.*string/) {   # no modifiers in regexp
    print "First \"if\" loop (without modifier) matched.\n";
} else {
    print "First \"if\" loop (without modifier) didn't match.\n";
}

if ($str =~ /this.*string/s) {   # s modifier in regexp
    print "Second \"if\" loop (with s modifier) matched.\n";
} else {
    print "Second \"if\" loop (with s modifier) didn't match.\n";
}
0
 
mjcoyneCommented:
The answer is there -- mgh_mgharish's regex ($str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"]?)(.+?)\1[^>]*>.*?</a>\s*\[/img\]#<img href="$2">#ims;) works beautifully; he should get the points.
0
 
DzejmsAuthor Commented:
Sorry, forgot about this one.  Ended up going another route.  Points for you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.