Regular expression help !!

Regular expression help !!

am 27.04.2011 12:47:56 von jet speed

Hi,

Please could you advice, how can i write a regular expression for the
line below to capture 0079 and 6000097000029260057253303030373


0079 Not Visible 6000097000029260057253303030373

i tried this one, no luck

/(^\d{4})\s\w+\s\w+\s+\d+/ig)


Appreciate your help with this.

Sj

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

Re: Regular expression help !!

am 27.04.2011 13:38:01 von Paolo Gianrossi

--bcaec5016151b020d404a1e4e093
Content-Type: text/plain; charset=ISO-8859-1

2011/4/27 jet speed

> Hi,
>
> Please could you advice, how can i write a regular expression for the
> line below to capture 0079 and 6000097000029260057253303030373
>
>
> 0079 Not Visible 6000097000029260057253303030373
>
> i tried this one, no luck
>
> /(^\d{4})\s\w+\s\w+\s+\d+/ig)
>
>
>
What about
/^(\d+)\D+(\d+)$/ ?



> Appreciate your help with this.
>
>
Not sure wether this is actually what you need, though


> Sj
>
>
cheers
paolino


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

--bcaec5016151b020d404a1e4e093--

Re: Regular expression help !!

am 27.04.2011 14:09:55 von Shawn H Corey

On 11-04-27 06:47 AM, jet speed wrote:
> 0079 Not Visible 6000097000029260057253303030373

Try this:

#!/usr/bin/env perl

use strict;
use warnings;

my $text = '0079 Not Visible 6000097000029260057253303030373';

my @numbers = $text =~ /(\d+)/g;
print "@numbers\n";

__END__


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: Regular expression help !!

am 27.04.2011 14:11:35 von Jeff Pang

2011/4/27 jet speed :
> Hi,
>
> Please could you advice, how can i write a regular expression for the
> line below to capture 0079 and 6000097000029260057253303030373
>
>
> 0079 Not Visible             60000970000292=
60057253303030373
>


This might help?

$ perl -le '
$str=3D"0079 Not Visible 6000097000029260057253303030373";
@re =3D $str =3D~ /^(\d+).*?(\d+)$/;
print "@re"'

0079 6000097000029260057253303030373

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

Re: Regular expression help !!

am 27.04.2011 16:53:00 von Rob Dixon

On 27/04/2011 11:47, jet speed wrote:
>
> Please could you advice, how can i write a regular expression for the
> line below to capture 0079 and 6000097000029260057253303030373
>
>
> 0079 Not Visible 6000097000029260057253303030373
>
> i tried this one, no luck
>
> /(^\d{4})\s\w+\s\w+\s+\d+/ig)

It works fine! You are simply not capturing the second sequence of
digits. This

use strict;
use warnings;

for ('0079 Not Visible 6000097000029260057253303030373') {
print /(^\d{4})\s\w+\s\w+\s+(\d+)/ig ? "$1 $2" : 'no match';
}

prints '0079 6000097000029260057253303030373'.

But, depending on your data, there may be no need to match the line so
precisely. This does the same thing

use strict;
use warnings;

for ('0079 Not Visible 6000097000029260057253303030373') {
my @n = (split)[0,-1];
print "@n";
}


HTH,

Rob

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

Re: Regular expression help !!

am 27.04.2011 17:32:57 von jet speed

Hi all,

Thanks for all our inputs,

The regular expression below works fine if do it for single line, i am
trying to caputre the match $1, and $2 into array. only the first line
is pushed to the array. what am i doing wrong ?
how to get all the $1 and $2 match values for each line into arrary ?
Kindly advice.


# more wwnlist
0079 Not Visible 6000097000029260057253303030373
007A Not Visible 6000097000029260057253303030374
007B Not Visible 6000097000029260057253303030374
007C Not Visible 6000097000029260057253303030374
007D Not Visible 6000097000029260057253303030374
007E Not Visible 6000097000029260057253303030374
more wwnmod.pl
#!/usr/bin/perl

#0079 Not Visible 6000097000029260057253303030373


open(FILE, "wwnlist") || die "Can't open wwnlist : $!\n";
while () {
if ($_=~/\b(\d{4})\s[a-z]{1,3}\s[a-z]{1,7}\s*(\d{1,32})\b/i) {
push (@dev, $1);
push (@wwn, $2);
}
}

print "@dev \n";
print "@wwn \n";

../wwnmod.pl
0079
6000097000029260057253303030373

Regards

Sj


On 4/27/11, Rob Dixon wrote:
> On 27/04/2011 11:47, jet speed wrote:
>>
>> Please could you advice, how can i write a regular expression for the
>> line below to capture 0079 and 6000097000029260057253303030373
>>
>>
>> 0079 Not Visible 6000097000029260057253303030373
>>
>> i tried this one, no luck
>>
>> /(^\d{4})\s\w+\s\w+\s+\d+/ig)
>
> It works fine! You are simply not capturing the second sequence of
> digits. This
>
> use strict;
> use warnings;
>
> for ('0079 Not Visible 6000097000029260057253303030373') {
> print /(^\d{4})\s\w+\s\w+\s+(\d+)/ig ? "$1 $2" : 'no match';
> }
>
> prints '0079 6000097000029260057253303030373'.
>
> But, depending on your data, there may be no need to match the line so
> precisely. This does the same thing
>
> use strict;
> use warnings;
>
> for ('0079 Not Visible 6000097000029260057253303030373') {
> my @n = (split)[0,-1];
> print "@n";
> }
>
>
> HTH,
>
> Rob
>

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

Re: Regular expression help !!

am 27.04.2011 18:47:59 von Jim Gibson

On 4/27/11 Wed Apr 27, 2011 8:32 AM, "jet speed"
scribbled:

> Hi all,
>
> Thanks for all our inputs,
>
> The regular expression below works fine if do it for single line, i am
> trying to caputre the match $1, and $2 into array. only the first line
> is pushed to the array. what am i doing wrong ?
> how to get all the $1 and $2 match values for each line into arrary ?
> Kindly advice.
>
>
> # more wwnlist
> 0079 Not Visible 6000097000029260057253303030373
> 007A Not Visible 6000097000029260057253303030374
> 007B Not Visible 6000097000029260057253303030374
> 007C Not Visible 6000097000029260057253303030374
> 007D Not Visible 6000097000029260057253303030374
> 007E Not Visible 6000097000029260057253303030374
> more wwnmod.pl
> #!/usr/bin/perl
>
> #0079 Not Visible 6000097000029260057253303030373
>
>
> open(FILE, "wwnlist") || die "Can't open wwnlist : $!\n";
> while () {
> if ($_=~/\b(\d{4})\s[a-z]{1,3}\s[a-z]{1,7}\s*(\d{1,32})\b/i) {
> push (@dev, $1);
> push (@wwn, $2);
> }
> }
>
> print "@dev \n";
> print "@wwn \n";
>
> ./wwnmod.pl
> 0079
> 6000097000029260057253303030373
[
The metasymbol \d matches the characters [0-9], not the extended hexadecimal
set that includes A-Z. To match those, construct your own character class:

[0-9A-Z]

So your regular expression test will become:

if ($_=~/\b([0-9A-Z]{4})\s[a-z]{1,3}\s[a-z]{1,7}\s*(\d{1,32})\b /i) {




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

Re: Regular expression help !!

am 27.04.2011 18:50:35 von Paul Johnson

On Wed, Apr 27, 2011 at 04:32:57PM +0100, jet speed wrote:
> Hi all,
>
> Thanks for all our inputs,
>
> The regular expression below works fine if do it for single line, i am
> trying to caputre the match $1, and $2 into array. only the first line
> is pushed to the array. what am i doing wrong ?
> how to get all the $1 and $2 match values for each line into arrary ?
> Kindly advice.
>
>
> # more wwnlist
> 0079 Not Visible 6000097000029260057253303030373
> 007A Not Visible 6000097000029260057253303030374
> 007B Not Visible 6000097000029260057253303030374
> 007C Not Visible 6000097000029260057253303030374
> 007D Not Visible 6000097000029260057253303030374
> 007E Not Visible 6000097000029260057253303030374

> if ($_=~/\b(\d{4})\s[a-z]{1,3}\s[a-z]{1,7}\s*(\d{1,32})\b/i) {

\d doesn't match hex digits or, at least not all of them.

Instead of your first \d you could use [0-9A-F] which actually matches exactly
the characters you want. \d will also match many characters you probably
don't want to match, unfortunately.

However, I might be tempted to go with Rob's second solution, unless you are
sure each line matches that regexp exactly. And if you are, is it always "Not
Visible" in there, or can it really be any three and seven letter words? If
not, why not use "Not Visible" in your regexp?

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

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

Re: Regular expression help !!

am 27.04.2011 19:11:36 von rvtol+usenet

On 2011-04-27 18:47, Jim Gibson wrote:

> The metasymbol \d matches the characters [0-9],

Beware: the \d matches 250+ code points. So don't use \d if you only
mean [0-9].


> not the extended hexadecimal
> set that includes A-Z. To match those, construct your own character class:
>
> [0-9A-Z]

Or use [[:xdigit:]], though that also matches the lowercase a-f.

(I assume that you meant A-F)

--
Ruud

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

Re: Regular expression help !!

am 27.04.2011 19:48:58 von Shawn H Corey

On 11-04-27 12:47 PM, Jim Gibson wrote:
> The metasymbol \d matches the characters [0-9], not the extended hexadecimal
> set that includes A-Z. To match those, construct your own character class:
>
> [0-9A-Z]

You can use the POSIX xdigit character class instead:

#!/usr/bin/env 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;

while( ){
my @hex_numbers = ( m{ ( [[:xdigit:]]+ ) }gmsx )[ 0, -1 ];
print '@hex_numbers: ', Dumper \@hex_numbers;
}

__DATA__
0079 Not Visible 6000097000029260057253303030373
007A Not Visible 6000097000029260057253303030374
007B Not Visible 6000097000029260057253303030374
007C Not Visible 6000097000029260057253303030374
007D Not Visible 6000097000029260057253303030374
007E Not Visible 6000097000029260057253303030374


See `perldoc perlreref` and search for /xdigit/


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: Regular expression help !!

am 27.04.2011 22:29:42 von Brian Fraser

--0015175cd8d2f7a2ff04a1ec4c39
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Apr 27, 2011 at 2:48 PM, Shawn H Corey wrote:

> On 11-04-27 12:47 PM, Jim Gibson wrote:
>
>> The metasymbol \d matches the characters [0-9], not the extended
>> hexadecimal
>> set that includes A-Z. To match those, construct your own character class:
>>
>> [0-9A-Z]
>>
>
> You can use the POSIX xdigit character class instead:

Or the Unicode character propriety[0] \p{Hex} (or \p{Hex_Digit} or whichever
form you prefer the most).

Or, since the data seems to always come in the same form, instead of using a
regex, you could pull out unpack [1]:
use 5.010;

while () {
say join ',', (unpack "A5 A24 A*")[0,-1];
}

You can learn more about pack/unpack in perlpacktut[2]

Or you could use substr[3]:
while () {
chomp;
say join ',', substr($_, 0, 4), substr($_, 29);
}

Brian.

[0] http://perldoc.perl.org/perluniprops.html
[1] http://perldoc.perl.org/functions/unpack.html
[2] http://perldoc.perl.org/perlpacktut.html
[3] http://perldoc.perl.org/functions/substr.html

--0015175cd8d2f7a2ff04a1ec4c39--

Re: Regular expression help !!

am 27.04.2011 23:31:59 von jet speed

--e0cb4e385316bd847904a1ed2b7f
Content-Type: text/plain; charset=ISO-8859-1

Excellent Guys, I would like thank each one of you for inputs. Much
appreciated.

i got blinded by just the numbers 0079, i didn't cater for the next line
which is hex 007A, as one of you rightly pointed out [ 0-9A-Z] , does the
trick. its amazing to see different technique to achieve the same result.
Thanks again.

Sj

--e0cb4e385316bd847904a1ed2b7f--