Solved

Regular Expression Question

Posted on 2006-06-30
13
275 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

14 Experts available now in Live!

Get 1:1 Help Now