Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

simple regex question

by indapa (Monk)
on Mar 03, 2001 at 06:58 UTC ( #61955=perlquestion: print w/replies, xml ) Need Help??

indapa has asked for the wisdom of the Perl Monks concerning the following question:

PerlMonks, I have simple regular expression question for you. I am reading a file and if a line starts with the number 1, I want to write that line to a file. The problem is that if that file starts with 1, 11, 12, etc. those lines get written that file too. My regular expressions is like this:
if (/^1/) { print FILE, "$_\n"; }
Is there a way to make my regex more stringent so I don't get lines in my file I don't want? Thank you.

Replies are listed 'Best First'.
Re: simple regex question
by beowulf (Scribe) on Mar 03, 2001 at 07:31 UTC
    Assuming that you want just the lines that have 1 and then whitespace after that, and then text the following code will work

    if (/^1\s+/) { print FILE, "$_\n"; }
    Life? What's a life?
      A more general solution might be /^1\b/. Of course, the previous suggestion will almost certainly work also, but in the spirit of TIMTOWTDI, and not fully knowing the format of the data..

      perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

        A more general solution might be /^1\b/.

        ...or, borrowing from Tuna below: /^1\D/.

        I was thinking about that too, actually
        Life? What's a life?
      Life? What's a life?
      anagram of a file? an ordered sequence ending with EOL?
Re: simple regex question
by Tuna (Friar) on Mar 03, 2001 at 07:41 UTC
    Here's a simple one, that seems to cover it:

    The "data" is a file called "lines":

    1 flew over the cuckoos nest
    12 flew over the cuckoos nest
    #!/usr/bin/perl -w use strict; my $line = "/home/trix/lines"; open (L, "$line") || die "$!\n"; while (my $lines = <L>) { chomp $lines; if ($lines =~ /^1{1}\D/) { print "my lines are $lines\n"; } else { print "Nope!\n"; } }

    Update: this regex doesn't catch lines that begin with "1d", for instance. Take other's suggestions.
Re: simple regex question
by arturo (Vicar) on Mar 03, 2001 at 23:14 UTC

    Just for fun:

    while (<FILE>) { print if ( /^(\d+)/ && $1 == 1); }

    Oughta do the job too.

    (Explanation: if the expression matches, i.e. if there are one or more digits at the beginning of the line, then that sequence of digits will be saved in $1, and then the right-hand-side of the conjunction will be evaluated).

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: simple regex question
by Madams (Pilgrim) on Mar 04, 2001 at 10:14 UTC
    Tilly to the rescue....
    I had a nearly identical problem here it is and tilly's solution:
    my substitution problem

    Update:fixed the messed up link thanks a and Corion :)
    (__) (\/) /-------\/ / | 666 || * ||----||
Re: simple regex question
by indapa (Monk) on Mar 04, 2001 at 01:03 UTC
    Thanks for the responses, the following regex did the job:
    if (/\b^1\b/) { print FILE, "$_\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://61955]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-12-05 17:37 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (151 votes). Check out past polls.