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:
extracting a "backslash" character

 



ahammad1
New User

May 25, 2010, 6:17 AM

Post #1 of 6 (7069 views)
extracting a "backslash" character Can't Post

Hello,

I have a string that may look something like the following:

fdhkhjgrrmewdDFSdsffewij\xF4dfdfewfefrere

I'm interested in extracting the \x along with the 2 characters that follow. In the above example, I would like to get a handle on "\xF4".

For my application, the \x indicates that the next 2 characters are a hex value. I will then use chr(hex(...)) to get the character equivalent. How can I do this using regex?

I tried /\\x(..)/ but I don't get any matches. I know that the \x is reserved within Perl. How can I go around it?

Cheers


shawnhcorey
Enthusiast


May 25, 2010, 7:55 AM

Post #2 of 6 (7066 views)
Re: [ahammad1] extracting a "backslash" character [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

my $str = 'fdhkhjgrrmewdDFSdsffewij\xF4dfdfewfefrere';

my @hex_seqs = $str =~ m{ ( \\ x [[:xdigit:]][[:xdigit:]] ) }msx;
print '@hex_seqs ', Dumper \@hex_seqs;


See:
perldoc perlretut
perldoc perlre

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


ahammad1
New User

May 25, 2010, 11:58 AM

Post #3 of 6 (7057 views)
Re: [shawnhcorey] extracting a "backslash" character [In reply to] Can't Post

Hello Shawn,

I tried it but it did not work. The output was:

@hex_seqs $VAR1 = [];

Do you have any other suggestions? Basically, I want to replace the \xF4 with its character equivalent (in this case it would be "ô").

In Perl, if I print that string, the \x is automatically recognized as a hex handler, and the output is the correct character. However, I am not printing, but I am passing the result as a string to another application. The other application doesn't recognize the \x, so it prints the string as is. That is why I want to handle this on the Perl side before passing the result.


shawnhcorey
Enthusiast


May 25, 2010, 12:18 PM

Post #4 of 6 (7056 views)
Re: [ahammad1] extracting a "backslash" character [In reply to] Can't Post

OK, try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

my $str = "fdhkhjgrrmewdDFSdsffewij\xF4dfdfewfefrere";

print "$str\n";
my @list = $str =~ m{ ( [\x7f-\xff] ) }gmsx;
print '@list ', Dumper \@list;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


ahammad1
New User

May 25, 2010, 12:55 PM

Post #5 of 6 (7053 views)
Re: [shawnhcorey] extracting a "backslash" character [In reply to] Can't Post

Oh, I see what you have done there. Clever!

Now I have another question. This regex pattern does retrieve the right character. Is there a way to use variables within the regex? For example, once the regex is run, the list would have all the characters. Can we replace the whole "\xF4" with the character?

For instance, if I have:

1234ABCD\xF4ABCD234

I want it to be:

1234ABCDôABCD1234

Cheers, and thanks again for the suggestion!


shawnhcorey
Enthusiast


May 25, 2010, 1:47 PM

Post #6 of 6 (7049 views)
Re: [ahammad1] extracting a "backslash" character [In reply to] Can't Post

You should start a new thread for a new question. That way, more people will see it. If you add it to the end of an existing question, you those that are subscribed tot he thread will see it.

I think you want something like this (untested):

Code
$str =~ s{ \\ x ( [[:xdigit:]][[:xdigit:]] ) }{ chr( hex( $1 )) }egmsx;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


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

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