Welcome to WebmasterWorld Guest from 54.242.138.24

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Regular expression help with ".+"

     

doctormelodious

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

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

Thanks,
Perry

doctormelodious

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

10+ Year Member



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

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

10+ Year Member



Found the problem! It works like a charm now.

Thanks!
Perry

markanthony

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

10+ Year Member



I am glad I could help.

doctormelodious

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

10+ Year Member



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

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

10+ Year Member



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

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

10+ Year Member



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

--Perry

 

Featured Threads

Hot Threads This Week

Hot Threads This Month