in reply to Re^2: Best practice validating numerics with regex?
in thread Best practice validating numerics with regex?
It's fairly easy to create a single high-performance regex that will capture the first (or every) valid float in a string. I would think that the main reason to use two regexes (one to cast a broad net, and one to validate it) would be to helpfully report syntax errors instead of skipping over them and reporting a more generic error. Is that why you're trying to do this?
I'm also not clear on your question, really. (but, I also don't have the book you are referencing)
my $lookAhead = qr/ (?! (?: .*\.){2,}) /x; my $regex = qr/ ^ $lookAhead [+-]? [\d.]+ $/x; ... for my $str (@strings) { say "\$str => $str"; if ($str =~ / [+-]?[\d.]+ /x) { # Pattern fails without this step +; why??? if ($& =~ $regex) {
Your $regex uses '^' and '$', so of course you would need to load the digits into an isolated string first, so I'm guessing I don't understand the question. Could you show an example of the code construct that fails that you think should succeed?
|
|---|