Solved

Regular Expression Question

Posted on 2006-06-30
13
286 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

861 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

23 Experts available now in Live!

Get 1:1 Help Now