Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Regex condition

by Win (Novice)
on Oct 23, 2007 at 13:54 UTC ( #646686=perlquestion: print w/replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:


I've written this but it is not processing the lines correctly.
if ($_ =~ /([\d]+)\t ([^\t]+)\t ([^\t]+)\t ([^\t]+)\t ([^\t]+)\t ([^\t]+)\t ([^\t]+)\t ([^\t]+)\n /x){
Can anyone please tell me if this bit of code is 'out of the ordinary' or not.

Replies are listed 'Best First'.
Re: Regex condition
by gamache (Friar) on Oct 23, 2007 at 14:00 UTC
    It looks like you want to be using split, maybe like:
    my @fields = split /\t/, $_;

      You forgot to remove the trailing newline. (Note the \n in the OP's regex.)

      chomp; my @fields = split /\t/, $_;

      And if you want to keep empty trailing fields, you'll need to specify the third parameter.

      chomp; my @fields = split /\t/, $_, -1;
Re: Regex condition
by ikegami (Pope) on Oct 23, 2007 at 14:24 UTC
    Hi Win. Line 20 of your input is malformed. You can tell from the second error message.
Re: Regex condition
by johngg (Abbot) on Oct 23, 2007 at 15:10 UTC
    I don't know what the problem is since you don't give us enough information to go on, such as, what are "the lines" you are matching against? However, two things about your code occur to me.

    When you are matching against something in $_, you don't have to say $_ =~ /.../ but can just use /.../ as the match will be against $_ by default.

    You don't have to put square brackets around the \d as it is already a character class denoting digits.



Re: Regex condition
by mwah (Hermit) on Oct 23, 2007 at 15:20 UTC
    I've written this but it is not processing the lines correctly.

    win, there has been a reply to your post: how to split this on tabs, but if its required to do with regexes, your project condenses to sth. like this:

    ... open my $filehandle, '<', 'win.ini' or die "with style $!"; while( <$filehandle> ) { if( (my @fields = /^\d+ | [^\t]+/xg) == 8 ) { print "all 8 elements found: @fields\n" } } ...



    correction: [^\t] replaced [^\s], this is what I used in a small test run (and forgot to modify after this.)

Re: Regex condition
by roboticus (Chancellor) on Oct 24, 2007 at 00:43 UTC

    You might wanna change the '\t's in your parenthesis to '\d's instead. Right now your regex is looking for a number followed by a mess of tabs....followed by newline.


    Update: Need new glasses...

      I think that ([^\t]+) means anything that is not a tab any number of times.

        nit: any number of -> one or more

        non-Perl: Andy Ford


        You are absolutely correct. I need to get my eyes checked, as I didn't notice you were using a character class.

        In any case, you'll still want to follow the advice of the other monks and consider using split.


Re: Regex condition
by GrandFather (Sage) on Oct 24, 2007 at 01:29 UTC

    Do you really want to be using Text::CSV or Text::xSV?

    Perl is environmentally friendly - it saves trees

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://646686]
Approved by andyford
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-05-22 20:33 GMT
Find Nodes?
    Voting Booth?