Solved

Regular Expression Question

Posted on 2006-06-30
13
308 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:Dzejms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
13 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020600
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020604
add the ims options...

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

Expert Comment

by:Harisha M G
ID: 17020611
A small tweak:

$str =~ s#\[img\]\s*<a\s+href\s*=\s*(['"]?)(.+?)\1[^>]*>.*?</a>\[/img\]#<img href="$2">#ims;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Accepted Solution

by:
Harisha M G earned 500 total points
ID: 17020633
Another (preferably final) tweak..

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

Author Comment

by:Dzejms
ID: 17020634
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020672
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17020879
.. and in which language are you writing the code ?
0
 

Author Comment

by:Dzejms
ID: 17021043
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17021066
Nope.. just asked the language of your code.. not the code itself :)
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17023974
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
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17174717
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
 

Author Comment

by:Dzejms
ID: 17175331
Sorry, forgot about this one.  Ended up going another route.  Points for you!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question