CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Regex, multiple matches, loop

 



Spinner
Novice

Feb 5, 2011, 9:32 AM

Post #1 of 4 (3016 views)
Regex, multiple matches, loop Can't Post

I have a string which can look like this:

Lorem ipsum dolor sit amet, @John Doe@ consectetur adipiscing elit. Donec porta lorem @Jane Doe@ fermentum urna laoreet hendrerit. Maecenas @Mac Bob@ nisi mi, rutrum nec luctus at, tincidunt non eros.

What I want it to look like is simply

Lorem ipsum dolor sit amet, [name]12345:John Doe[/name] consectetur adipiscing elit. Donec porta lorem [name]23456:Jane Doe[/name] fermentum urna laoreet hendrerit. Maecenas [name]34567:Mac Bob[/name] nisi mi, rutrum nec luctus at, tincidunt non eros.

For any lazy folks out there, I have replaced @<data>@ with [name]12345:<data>[/name] .

Now, the regex for doing that switch is of course not a problem. The problem is that I do now know the numbers that I want to insert before I have "processed" the match itself (checking the name against a database and then finding the number I need to put in there, which is not a problem in itself).

So how would I go about doing something like this:
Find all matches, then, for each match, look up the name in the DB to find the number, and then replace the match with my altered string?

Is there a way a matching-regex can give me multiple results in an array, so to speak, for traversing?

Any help would be much appreciated.

- Spinner


Spinner
Novice

Feb 5, 2011, 5:10 PM

Post #2 of 4 (3005 views)
Re: [Spinner] Regex, multiple matches, loop [In reply to] Can't Post

Never mind, I figured it out in the end :)
I was only a set of brackets away from the solution, hehe.


BillKSmith
Veteran

Feb 6, 2011, 12:24 PM

Post #3 of 4 (2975 views)
Re: [Spinner] Regex, multiple matches, loop [In reply to] Can't Post

I did not see your second post soon enough. I had to simulate your data base before I could test the RE. Did you think to execute the db from within the RE? A single substitution statement does the whole job. I cannot post it because the forum does not render it correctly. I will attach it and hope that you can get that correctly.
Good Luck,
Bill
Attachments: spinner.pl (0.79 KB)


Spinner
Novice

Feb 6, 2011, 2:48 PM

Post #4 of 4 (2968 views)
Re: [BillKSmith] Regex, multiple matches, loop [In reply to] Can't Post

 
Thank you :)

My solution looks like this actually:

Code
sub pre_write_parse{ 
my $dbh=shift; # My database handle
my $string=shift; # The incoming string that needs checking
my @matches = ( $string =~ /\@([^\@]+)\@/gi); # And here were the brackets I was missing, hehe
for (my $i=0;$i<=$#matches ;$i++) {
$matches[$i]=mylib::string::strict_wash_allow_space($matches[$i]); # just a security-precaution
my $sth = $dbh->prepare("select userId from users where nickname='$matches[$i]'");
my @user = mylib::db::fetch_data( $sth );
if ($#user>-1) {
$string =~ s/\@$matches[$i]\@/\[namelink\]$user[0]{userId}\:\:$matches[$i]\[\/namelink\]/gi;
}
}
return $string;
}

I know this could be potentially optimized by checking if some of the matcheas are identical to avoid doing the lookup and replcements twice, but it's not a big deal in this case.
Which solution is "faster" or "better" I don't know, but mine works for me in this case...Unless you have a comment to make me reconsider :)

Thanks again!

- Spinner

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives