Solved

Regular Expression Question

Posted on 2006-06-30
13
292 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 92
Check file date before getting folder path 1 72
perl to convert excel to csv 3 206
Using Perl DBI to query oracle 3 40
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now