|
Posted by John W. Krahn on January 25, 2008, 2:39 pm
Please log in for more thread options
John wrote:
> I have text that might have had a star character in the proprietary
> orginating system. The character is used in ratings boxes: A three-
> star movie, a four-star restaurant, etc.
>
> By the time it's exported and available to me, it's represented by a
> string: "<star>".
>
> I want to suround consecutive stars with font coding and replace each
> instance of the string with a single character that, in conjuction
> with the font change, will eventually print as a star.
>
> To set up this substitution, I change the strings back to a unique
> character, one that I reckon would never occur in nature.
>
> When I try to surround any repetitions of this invented character, I
> instead match everything.
>
> ===
>
> #!/usr/bin/perl -w
> use strict;
>
> my $text = "Cuisine: Urban deli<ep>";
> $text .= "Overall: <star><star><star><star><1/2> (very good to
> excellent)<ep>";
> $text .= "Food: <star><star><star><star><1/2><ep>";
>
> $text =~ s/\<star\>/_STAR_/ig; # uscores easier in regex than angle
> brackets.
> $text =~ s/_STAR_/\xbc/g; # change pseudocharacter to single
> character
> $text =~ s/(\xbc*)/_STARFONT_$1_ENDSTAR/g; #bracket groups in more
> pseudocode
>
> print $text;
>
> ====
>
> If I limit the search to five consecutive stars, the match works as I
> intended:
>
> ===
>
> #!/usr/bin/perl -w
> use strict;
>
> my $text = "Cuisine: Urban deli<ep>";
> $text .= "Overall: <star><star><star><star><1/2> (very good to
> excellent)<ep>";
> $text .= "Food: <star><star><star><star><1/2><ep>";
>
> $text =~ s/\<star\>/_STAR_/ig; # uscores easier in regex than angle
> brackets.
> $text =~ s/_STAR_/\xbc/g; # change pseudocharacter to single
> character
> $text =~ s/(\xbc)/_STARFONT_$1_ENDSTAR/g; #bracket groups of
> stars in more pseudocode
>
> print $text;
>
> ===
>
> So what am I missing when it comes to the first search?
>
> Certainly, I am missing some superior technique for matching repeated
> instances of such a string, so I am open to suggestions there.
In the first regular expression you are matching '\xbc*' and in the
second you are matching '\xbc'. The '*' modifier matches *zero* or
more times and there are *zero* '\xbc' characters everywhere in the
string. The second one has to match at least *one* character. Change
'\xbc*' to '\xbc+'.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|