Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help on building regex

Reply
Thread Tools

Need help on building regex

 
 
doni
Guest
Posts: n/a
 
      12-21-2006
Hi all,

I'm new to perl & also new to regex. Could u all please help me
building a regex to do the following:

I have text files with lines like these:
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
e, f are numerics.

Any help will be much appreciated.

 
Reply With Quote
 
 
 
 
tfe
Guest
Posts: n/a
 
      12-21-2006
Hello,

if the numbers are at the end of each line , you can use: the regular
expression /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/

you will get ($1,$2,$3,$3,$4,$5,$6) containing a b c d e f.


doni a écrit :

> Hi all,
>
> I'm new to perl & also new to regex. Could u all please help me
> building a regex to do the following:
>
> I have text files with lines like these:
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
>
> I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
> e, f are numerics.
>
> Any help will be much appreciated.


 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      12-21-2006
doni wrote:
> I'm new to perl & also new to regex. Could u all please help me
> building a regex to do the following:
>
> I have text files with lines like these:
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
>
> I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
> e, f are numerics.


You wont need a regex for fixed-format stuff
like that, a simple 'split' will do.

You'll need to learn:
- 1) open a file
- 2) read from the file
- 3) split the line read into fields
- 3) put the fields into an array

Here's one Idea (try to figure out the steps):

---8<----

use strict;
use warnings;

my $text = q'
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
';

my @array;

open(my $fh, '<', \$text) or die "without a blink $!";
while( <$fh> ) {
push @array, [ (split) [5..10] ];
}
close $fh;

for my $row (@array) {
print join ',', @$row;
print "\n";
}
---8<----

Regards

Mirco


 
Reply With Quote
 
Doni Wahyudi
Guest
Posts: n/a
 
      12-22-2006
many thanks tfe, it works.

thanks to you too wahab. good idea..

On Dec 21, 10:10 pm, "tfe" <(E-Mail Removed)> wrote:
> Hello,
>
> if the numbers are at the end of each line , you can use: the regular
> expression /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/
>
> you will get ($1,$2,$3,$3,$4,$5,$6) containing a b c d e f.
>
> doni a écrit :
>
> > Hi all,

>
> > I'm new to perl & also new to regex. Could u all please help me
> > building a regex to do the following:

>
> > I have text files with lines like these:
> > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
> > BLR:39/SUNBURST:0 60 1 0 0 a b c d e f

>
> > I want to read the value of a, b, c, d, e, f into an array. a, b, c, d,
> > e, f are numerics.

>
> > Any help will be much appreciated.


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-22-2006
doni schreef:

> I have text files with lines like these:
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
> BLR:39/SUNBURST:0 60 1 0 0 a b c d e f
>
> I want to read the value of a, b, c, d, e, f into an array.
> a, b, c, d, e, f are numerics.


perl -wnle '
$, = "," ;
print +(/[0-9]+/g)[-6..-1]
' filename

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Connecting Wireless Network from Building to Building Jim Wireless Networking 5 10-05-2007 03:54 PM
Firefighters at the site of WTC7 "Move away the building is going to blow up, get back the building is going to blow up." Midex Python 24 05-07-2007 04:23 AM
Wireless building-to-building 101 Tim Jacob Wireless Networking 2 02-17-2006 09:46 AM
Building to Building wireless Patriot Cisco 2 11-04-2003 05:07 PM



Advertisments