tags:

views:

51

answers:

2
Q: 

regex help

I got an answer to my question here: http://stackoverflow.com/questions/699253/how-to-find-out-if-file-is-tab-or-space-delimited-in-perl/699256#699256

but it would really help me if someone could break down the regex and explain what is going on and why it wont work on the last line of the file.

~/^(\d+\s+)+?$/

I thought the above had '+' in theback so if i add '*' it will work because * means zero or more...but that did not work

A: 

In fact the original regex does not accept empty lines, that's probably why it does not work on the last line of your file (a link to an example file would be nice). It should look like this:

perl -ne 'if ($_=~/^(\d+\s+)*$/){print "yep\n";}'

Another way might be to just check for all the characters to be either whitespace or a digit.

When accepting empty lines:

perl -ne 'if ($_=~/^[\s\d]*$/){print "yep\n";}'

When not accepting empty lines:

perl -ne 'if ($_=~/^[\s\d]+$/){print "yep\n";}'
x-way
+1  A: 
Regex: /^(\d+\s+)+?$/
Parts:  1  2  3  456
  1. Match from the start of a line
  2. Find one or more numbers
  3. Followed by one or more spaces (or tabs)
  4. Find one or more of 2 and 3
  5. But don't be greedy in that match (that is, stop when you can, don't keep going until you can't)
  6. Match the end of a line.

It should match a string of an entire line of space or tab separated numbers. I'm not exactly sure about why it'd be failing on the last line.. perhaps there's no space character at the end? Since each number must be followed by at least one space, that might be it.

nickf