homepage Welcome to WebmasterWorld Guest from 23.20.220.79
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Regular expression help with ".+"
doctormelodious




msg:439425
 8:32 pm on Jun 21, 2004 (gmt 0)

Greetings,

I want to turn this string (entered in a form textArea):


`bhey` man `bwhassup`

into this:


<b>hey</b> man <b>whassup</b>

Here is my attempt in Perl, which isn't working right:


# TESTING EZ FORMATTING
$boldBegin = "`b";
$ezEnd = "`";

# $CONTENT IS FROM THE FORM TEXTAREA

while($theContent =~ /($boldBegin.+$ezEnd)/){

$found = $1;
$myFound = $found;
$theReplacement = $myFound;

$theReplacement =~ s/\`b//g;
$theReplacement =~ s/\`//g;
$theReplacement = "<b>" . $theReplacement . "</b>";

$theContent =~ s/$myFound/$theReplacement/im;
}

The problem is that $theFound contains the whole thing:


`bhey` man `bwhassup`

instead of just the first instance of $boldBegin.+$ezEnd, which would be


`bhey`

and the whole thing keeps looping.

How do I say "find these occurrences, however many there are, ONE AT A TIME and change them"?

Thanks much!
Perry

 

randallxski




msg:439426
 8:56 pm on Jun 21, 2004 (gmt 0)

Here's a solution. Let me know if I missed something in the requirements.

# TESTING EZ FORMATTING
$boldBegin = "`b";
$ezEnd = "`";
$theContent= "`bhey` man `bwhassup`";
# $CONTENT IS FROM THE FORM TEXTAREA

while($theContent =~ /($boldBegin.+$ezEnd)/){
$found = $1;
print $found . "\n";
$myFound = $found;
$theReplacement = $myFound;

$theReplacement =~ s/\`b/<b>/g;
$theReplacement =~ s/\`/<\/b>/g;

$theContent =~ s/$myFound/$theReplacement/im;
print $theContent . "\n";
}

markanthony




msg:439427
 9:16 pm on Jun 21, 2004 (gmt 0)

Try using a distinct pattern for the beginning of a bolded area and a distinct pattern for the end of a bolded area as opposed to just a distinct pattern for the beginning and just a ' at the end. You might as well use html at that point.

I becomes easy for you in the code if you can use say:

'b b' for start and end tags. It won't be perfect but it will solve the finding of end tags issue.

so your new code will look like this:

use strict;
my $boldBegin = "'b";
my $boldEnd = "b'";

$content =~ s¦$boldBegin¦<b>¦gi;
$content =~ s¦$boldEnd¦</b>¦gi;

#pipe symbol used so that there is no confusion with
#closing tags.

I may have oversimplified your issue. I am not sure if you are trying to do something bigger....

The reason the the .+ is grabbing the entire area is that the + quantifier is looking for as many of the given match as it can. You need to quantify with something more specific.

Match any but an end tag (in the context of your example)


use strict;

my $content="'bhey' man 'bwhassup'";

my $boldBegin = "'b";
my $ezEnd = "'";

$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;

print $content;

Again, I have changed the separators so that the code just looks clearer and we don't have to escape anything.

doctormelodious




msg:439428
 10:11 pm on Jun 21, 2004 (gmt 0)

Thanks for the help guys... still working on this...

By the way, how do I delete a post from this board?

Thanks,
Perry

doctormelodious




msg:439429
 10:46 pm on Jun 21, 2004 (gmt 0)

I should have explained that I want to provide other formatting options, with ` as the universal end tag. E.G.


`bhey` man `iwhassup`

would become


<b>hey</b> man <i>whassup</i>

Less typing, and no shift keys.

MarkAnthony,

Thanks much for the code.


my $content="'bhey' man 'bwhassup'";

my $boldBegin = "'b";
my $ezEnd = "'";

$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;

print $content;

It works when "my $content" = the literal string, but doesn't work when it = the form input (a textarea containing the same text as the literal).


my $content=$FORM{'content'};

my $boldBegin = "'b";
my $ezEnd = "'";

$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;

print $content; #PRINTS OUT `bhey` man `bwhassup`

Any thoughts on how I can correct this?

Thanks!
Perry

doctormelodious




msg:439430
 3:52 am on Jun 22, 2004 (gmt 0)

Found the problem! It works like a charm now.

Thanks!
Perry

markanthony




msg:439431
 8:42 pm on Jun 22, 2004 (gmt 0)

I am glad I could help.

doctormelodious




msg:439432
 8:12 am on Jun 26, 2004 (gmt 0)

Hi again MarkAnthony,

I have another question for you:


$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;

This works when $ezEnd is a single character (which is what I originally asked for). How could I modify this to allow for an $ezEnd that has more than one character?

Thanks again!
Perry

doctormelodious




msg:439433
 9:23 am on Jun 26, 2004 (gmt 0)

Specifically, I want to grab the found string in $1, do some stuff to it, then replace it.

Hence the need for something more than just changing all the b` to <b>, etc.

doctormelodious




msg:439434
 6:32 am on Jun 27, 2004 (gmt 0)

Never mind. I came up with a solution. :)

--Perry

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved