L~R, you are right with using anchors to limit the scope.
But your solution would only match a single digit instead of a number (not what the OP wanted).
One solution would be (not documented as of dragonchild's post):
/^(\d+|\?)?$/
++ for the right approach
Monkish Greetings,
Dietz
| [reply] [d/l] |
Dietz,
But your solution would only match a single digit instead of a number (not what the OP wanted). emphasis mine
Well see here is the quandry. You have interpreted it one way and I have another. Peamasii didn't define what exactly s/he meant by number. It could mean a single digit as I used, it could mean an integer which you described (which will match 0004 and miss -4 btw), or it could mean any number (pun intended) of things. For instance:
use Scalar::Util 'looks_like_number';
if ( looks_like_number( $foo ) && $foo =~ /^\??$/ ) {...}
It is probably as common a mistake to assume what you are thinking is universally understood when asking questions as it is for the person that is answering to assume they know what you mean. I think we both fall into the latter category this time.
| [reply] [d/l] |
Well, numbers fall into so many possible formats that it's impossible to catch them all. I'm going to assume that any integer or decimal format is allowed, and write the following:
foreach (<DATA>) { print $_ if $_ =~ /^(\?|-?\d*\.?\d+)?$/; }
__DATA__
?
3
3.3
.3
-3
-3.3
-.3
?
3-3
-
| [reply] [d/l] |
L~R, you are right again.
Didn't see it the way you described, especially when it comes to numbers with a minus before.
I was just recognizing the single digit match. And I was hesitating to post anyways (as of dragonchild's post above)
And I really didn't mean to offend you, I have great respect for you - as I have for most other monks here.
Respect,
Dietz
| [reply] |
You probably don't need the capturing parens. I'd write it as
/^(?:\d*|\?)$/
where the empty string is just a zero digit number, but that's just me. | [reply] [d/l] |