Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Do you know where your variables are?
 
PerlMonks  

Re^3: hex in regexp

by kcott (Abbot)
on May 22, 2012 at 12:39 UTC ( #971784=note: print w/ replies, xml ) Need Help??


in reply to Re^2: hex in regexp
in thread hex in regexp

Changing upper to ascii and using your string, I get:

$ perl -Mstrict -Mwarnings -E ' my $x = q{çe quil Y a Yå}; $x =~ s/([[:^ascii:]])/unpack q{H2}, $1/eg; say $x; ' c383c2a7e quil Y a Yc383c2a5

Check that you didn't make a typo when entering your code. If you are still having problems, please post your code - as it is, I can't reproduce your problem.

To wrap the codes in < and >, or any other characters, you can just concatenate the characters at the beginning and end of the hex code:

$ perl -Mstrict -Mwarnings -E ' my $x = q{çe quil Y a Yå}; $x =~ s/([[:^ascii:]])/q{<} . unpack(q{H2}, $1) . q{>}/eg; say $x; ' <c3><83><c2><a7>e quil Y a Y<c3><83><c2><a5>

-- Ken


Comment on Re^3: hex in regexp
Select or Download Code
Re^4: hex in regexp
by shamanoff (Initiate) on May 22, 2012 at 21:09 UTC
    I double checked the code and haven't found any typo:
    #!/usr/bin/perl $file = @ARGV[0]; $out_file = $file.".out"; my $count = 0; print "Processing file: $file\n"; print "Output file: $out_file\n"; open FILE, "$file" or die "cannot open $file file"; open OUT, ">>$out_file" or die "cannot create $out_file file"; #go through the file rows while (<FILE>) { chomp; # if row contain non-ASCII symbols - it is counted if ($_ =~ /[[:^ascii:]]/) { print "String before modification:\n $_ \n"; s/[[:^ascii:]]/q{<} . unpack(q{H2}, $1) . q{>}/eg; print "String after modification:\n $_ \n"; print OUT "$_\n"; $count++ ; } else { print OUT "$_\n"; } } print "there are $count possibly corrupted rows\n"; close FILE; close OUT;
    the contents of processed file:
    0,1,,,1,~=_<>}[]||$? £??§^`??¿ ?. Qu est çe quil Y a Yå,0,20120 +306101731 0,1,,,1,Teá,0,20120314104403 0,1,,,1,Q<8a>rUì ,0,20120306103345 1,1,,,3,,0,20120331152610 1,1,,,3,,0,20120331152612
Re^4: hex in regexp
by shamanoff (Initiate) on May 22, 2012 at 21:23 UTC
    Thanks, Ken! I found the typo - forgot to put the search expression in brackets ([...]). Let me say that I am sorry for my impatience. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971784]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-04-17 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls