Welcome to WebmasterWorld Guest from 54.160.254.203

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Regular expression help with ".+"

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 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

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

New User

10+ Year Member

joined:June 21, 2004
posts:33
votes: 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";
}

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

Junior Member

10+ Year Member

joined:May 14, 2004
posts:41
votes: 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.

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 0


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

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

Thanks,
Perry

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 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

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 0


Found the problem! It works like a charm now.

Thanks!
Perry

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

Junior Member

10+ Year Member

joined:May 14, 2004
posts:41
votes: 0


I am glad I could help.
8:12 am on June 26, 2004 (gmt 0)

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 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

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 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.

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

New User

10+ Year Member

joined:June 15, 2004
posts:33
votes: 0


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

--Perry