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:
Substitution not working in RegExp

 



LeoF
Novice

Aug 12, 2000, 6:32 PM

Post #1 of 3 (2114 views)
Substitution not working in RegExp Can't Post

Hello everyone,

I'm trying to remove leading and trailing spaces and blank lines from a string but is not working, is only doing it for the first match. Could someone be so kind as to give me any insight on this code?

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$var = "leave intact]


123";

$var2 = "123


[leave intact";

$var =~ s/(\] |\]\n)+/\]/g;
$var2 =~ s/( \[|\n\[)+/\[/g;

print "\$var = $var \n\n";
print "\$var2 = $var2 \n\n";
</pre><HR></BLOCKQUOTE>

I would like to remove every space and blank line after "]" and before "[" so that I get "leave intact]123" and "123[leave intact" as a result.

Any comments and suggestions would be greatly appreciated.

Thanks in advance for your cooperation.

Be Well


[This message has been edited by LeoF (edited 08-12-2000).]


japhy
Enthusiast / Moderator

Aug 12, 2000, 9:27 PM

Post #2 of 3 (2114 views)
Re: Substitution not working in RegExp [In reply to] Can't Post

Your problem is that the substitution starts where it left off.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str = "abXXXc";
$str =~ s/bX/b/g;
# $str is now "abXXc"
</pre><HR></BLOCKQUOTE>

This code doesn't remove all the X's after the b, only the first one:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# the [] means "the regex starts looking here"

string: [a]bXXXc
regex: s/bX/b/
match: abXXXc # changes 'bX' to 'b'

string: ab[X]Xc
regex: s/bX/b/
no match
</pre><HR></BLOCKQUOTE>

You would want something like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


1 while $str =~ s/bX/b/;
</pre><HR></BLOCKQUOTE>

But this can be optimized, since we can just say:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str =~ s/bX+/b/; # change a 'b' followed by 1 or more X's to 'b'
</pre><HR></BLOCKQUOTE>

We could tack on a /g modifier if there'd be more than one set of bXXX... in the string.

We can be even neater, and use a positive look-behind assertion:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str =~ s/(?<=b)X+//;
# change 1 or more X's to "" IF the first X is preceeded by a b
</pre><HR></BLOCKQUOTE>

Modify/alter to fit your code accordingly.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher

[This message has been edited by japhy (edited 08-12-2000).]


LeoF
Novice

Aug 13, 2000, 8:19 PM

Post #3 of 3 (2114 views)
Re: Substitution not working in RegExp [In reply to] Can't Post

Thanks a lot Jeff, I appreciate it, as always your post is very didactical, your explanation helped me to fix another regexp too.

I changed the code above to:

$var =~ s/\]\s+/\]/g;

and

$var2 =~ s/\s+\[/\[/g;

and it works as intended, I used \s to account for all white space, now I know where the + goes, thanks.

I have another regexp that is not working and I hope you can give me a hand with it:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$message = <<HTML;
<SPAN STYLE="font-size:80"><B> <SPAN STYLE="color:red">Testing mixed attributes @ 123</SPAN></B></SPAN>

Leave this text intact

<DIV STYLE="font-size:80"><B> <SPAN STYLE="color:red">Testing mixed attributes @ 123 </SPAN></B></DIV>
HTML

$message =~ s/(<SPAN STYLE="font-size : )(\d+)("> )(.+?)(<\/SPAN> )/\[size=$2\]$4\[\/size\]/isg;

$message =~ s/(<DIV STYLE="font-size : )(\d+)("> )(.+?)(<\/DIV> )/\[size=$2\]$4\[\/size\]/isg;

$message =~ s/(<SPAN STYLE="color : )(\S+)("> )(.+?)(<\/SPAN> )/\[color=$2\]$4\[\/color\]/isg;

$message =~ s/(<b> )(.+?)(<\/b> )/\[b\]$2\[\/b\]/isg;

print "message = \n $message \n\n\n";
</pre><HR></BLOCKQUOTE>

As a result I get:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


message =
[size=80] [color=red]Testing mixed attributes @ 123
[/color]

Leave this text intact

[size=80] [color=red]Testing mixed attributes @ 123 [/color]
</pre><HR></BLOCKQUOTE>

I was able to get this code to work after reading your post.

As you can see, in the fist case it mismatches the tags when I use SPAN for both size and color. If I change the tags for size to DIV and keep SPAN for color it works, but I wonder if I can make it account for nesting SPAN or other tags?

Any comments and suggestions would be greatly appreciated.

Thank you for your time and attention.

Be Well

(had to put some extra spaces to avoid smilies and bolding)


[This message has been edited by LeoF (edited 08-13-2000).]

 
 


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

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