Help regular expression - string paring

Help regular expression - string paring

am 04.05.2011 14:30:46 von loudking

Hi there,

Hereby I have a string parsing problem to ask. The sample strings look
like this:

00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............

What I am interested is the the 21st and 22nd byte value. In the above
examples, they are both 05.

Could anybody tell me how to retrieve that value out of the strings
using regular expression?

Thanks in advance!


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Help regular expression - string paring

am 05.05.2011 09:48:49 von Jeff Pang

2011/5/4 loudking :
> Hi there,
>
> Hereby I have a string parsing problem to ask. The sample strings look
> like this:
>
> 00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
> 00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............
>
> What I am interested is the the 21st and 22nd byte value. In the above
> examples, they are both 05.
>


This could work:

while() {
my ($mat) = /^.{20}(.{2})/;
print $mat,"\n";
}


--
Jeff Pang
www.DNSbed.com

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Help regular expression - string paring

am 05.05.2011 09:55:34 von Owen

> Hi there,
>
> Hereby I have a string parsing problem to ask. The sample strings look
> like this:
>
> 00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
> 00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............
>
> What I am interested is the the 21st and 22nd byte value. In the above
> examples, they are both 05.
>
> Could anybody tell me how to retrieve that value out of the strings
> using regular expression?
>
> Thanks in advance!

substr

#!/usr/bin/perl

use strict;

while () {
my $substring20 =3D substr( $_, 20, 1 );
my $substring21 =3D substr( $_, 21, 1 );
print "$substring20$substring21\n";
}

__DATA__
00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............



--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Help regular expression - string paring

am 05.05.2011 10:50:48 von Rob Dixon

On 04/05/2011 13:30, loudking wrote:
> Hi there,
>
> Hereby I have a string parsing problem to ask. The sample strings look
> like this:
>
> 00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
> 00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............
>
> What I am interested is the the 21st and 22nd byte value. In the above
> examples, they are both 05.
>
> Could anybody tell me how to retrieve that value out of the strings
> using regular expression?
>
> Thanks in advance!

First of all, it would be far better to open whatever is being dumped
here and evaluate the fifth byte directly, rather than dumping it and
parsing the dump. However I realise that there may be reasons why this
is not possible.

The program below uses unpack to extract two characters after the 20th
on each line that contains a colon.

HTH,

Rob


use strict;
use warnings;

while () {
next unless /:/;
my $byte = unpack '@20 A2';
print $byte, "\n";
}

__DATA__
00000000: CC02 0000 0565 0000 8E93 D501 0100 6273 .....e........b
00000000: 6800 0000 0500 0000 9093 D501 0100 1400 h...............

**OUTPUT**

05
05

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/