Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

problems with regular expressions

by jjdraco (Scribe)
on Oct 05, 2002 at 03:49 UTC ( #202986=perlquestion: print w/ replies, xml ) Need Help??
jjdraco has asked for the wisdom of the Perl Monks concerning the following question:

As I attempt to learn Perl I've looked over the internet several times trying to find programming examples to practice on. Well I cam across a website that has lots of challenging problems at ACM. The problem I'm working on is from they're '96 contest problem A. Well the problem I'm having is how they want it read it, I understand it as the want it read from the STDIN. The input is decks of cards and kings, queens, and jacks are all represented as the value 10 ace as the value 1. the input is supposed to be terminated by the number zero (0) but the problem didn't say it had to be on its own line. As a matter of fact it said that the cards for each deck read in could be seperated by a space or a newline. I've come up with some code that would work if i were reading it from a file, thats not the problem. but if i readd it from STDIN i can't seem to find the regex that would make the number zero (0) with out matching the number ten (10) as well. I've read through String matching and Regular Expressions and perlre, but i was unable to get clearification on this matter. i've also tried just experimenting, but the only thing i found was i can get find the zero as long as its not the first number in the string i.e.
while (<>) { print if(/\s0/); }
If I just use this:
while(<>) { print if(/0/); }
it prints the case when zero is the first number but it also prints when it finds any number with a 0 in it (i.e. 10, 20, 30)
I just can't figure it out. besides that i think i almost have the problem solved, besides a few bugs in the test case of a draw. but i have ideas of how i'm going to do it if i can just get the rest of the code working and this one thing is the only thing killing me right now.
The only other thing I can think to do is instead of reading it in a line at a time I could try reading in a charector at a time, but I'm not sure if that would be better or not.

jjdraco
learning Perl one statement at a time.

Comment on problems with regular expressions
Select or Download Code
Re: problems with regular expressions
by suaveant (Parson) on Oct 05, 2002 at 04:20 UTC
    well.. there are various things... such as \b, to match word boundaries... \D to match non-digit characters... \s to match whitespace (spaces tabs newlines)... \b is zero width so will work at the front of a string... there is also the ability to do things such as [\A\s]0 which matches the beginning of a string or a space followed by a zero... (i think that will work the way I mean)

    In this case though, \b is probably the way to go...

                    - Ant
                    - Some of my best work - (1 2 3)

      thanks, it works just the way I want it to work, I had seen that \b before, but it never really came to mind that thats what it did.

      jjdraco
      learning Perl one statement at a time.
Re: problems with regular expressions
by kelan (Deacon) on Oct 05, 2002 at 04:36 UTC
    Well, without more specifics as to when the zero will appear, try this:
    print if (/(^\s*|\s+)0/); # A zero either: starting a line with possi +ble whitespace in front, or anywhere in a line with ne +cessary whitespace in front.

    Update: suaveant's suggestion, above, is simpler and works just as well:   print if (/\b0/);

    kelan


    Yak it up with Fullscreen ChatterBox

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-09-18 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (120 votes), past polls