Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

regexp::assemble and dispatch-table with unicode

by ajan (Initiate)
on Jan 08, 2010 at 15:38 UTC ( #816332=perlquestion: print w/ replies, xml ) Need Help??
ajan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Im trying to make some regex mainly changing spaces between special words to be smaller spaces (needed for nice pdf printout).

Because there are many cases to match and I have do do it on large textfiles for each paragraph (depending on the individual style there are different sets of replacement) i'm just playing with regex::assemble.

Sadly, the script crashes when i try to replace space with eq Unicode Thin Space chr (0x2009).

Here is my code so far:

#!c:\strawberry\perl\bin\perl use strict; use warnings; use feature qw/:5.10/; use Regexp::Assemble; binmode( STDOUT, ":utf8" ); my $ra; say "SGB".chr(0x2009)."I"; #gives the expected output my %dispatch = ( '(SGB)[\s\xa0](I)' => sub { $ra->mvar(1) . chr(65) . $ra->mvar +(2) }, #works # '(SGB)[\s\xa0](I)' => sub { $ra->mvar(1) . chr(0x2009) . $ra- +>mvar(2) }, #crashes '(Art\.)[\s\xa0]?(\d)' => sub { $ra->mvar(1) . 'x' . $ra->mvar(2) +}, ); $ra = Regexp::Assemble->new( track => 1 )->add( keys %dispatch ); while (<DATA>) { while ( $ra->match($_) ) { my $m = $ra->matched; s/$m/&{$dispatch{$m}}/e; } print; } __DATA__ Dies ist ein Test für SGB I und spacing Art. 1 zum Beispiel ist hier es könnte aber auch Art 3 sein

I would be glad if i can get some enlightment from you

Thanks in advance

ajan

Update: just do say it correct, the script does not crash, it hangs (and i suppose it will hang forever...

Comment on regexp::assemble and dispatch-table with unicode
Download Code
Re: regexp::assemble and dispatch-table with unicode
by ikegami (Pope) on Jan 08, 2010 at 17:07 UTC
    U+2009 matches \s, so you go in an infinite loop.

    First pass of while loop: "SGB\x{0020}I" gets replaced with "SGB\x{2009}I"
    Second pass of while loop: "SGB\x{2009}I" gets replaced with "SGB\x{2009}I"
    Third pass of while loop: "SGB\x{2009}I" gets replaced with "SGB\x{2009}I"
    ...

    Sorry, I can't help find you a solution right now. I gotta run.

      Uups

      I was not aware of that

      Sometimes one can really be blind

      Thanks, that helps me further

      ajan

        RA has no obvious support for global searching. As a quick fix, you could replace \s with something more specific.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816332]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-08-28 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls