in reply to Re: Recognizing 3 and 4 digit number
in thread Recognizing 3 and 4 digit number
G'day Dave,
At first glance, I thought your regex was better than mine and so I decided to try it. I plugged it into my code but it failed on the phone number and date tests (details in spoiler). The OP requirements are not the best but excluding phone numbers and dates seems to be definitely wanted.
The code changed as follows:
#my $re = qr{(?x: (?<![/-]) \b ( [0-9]{3,4} ) \b (?![/-]) )}; my $re = qr{(?ax: (?<!\d) (\d{3,4}) (?!\d) )};
Output:
1..10 ok 1 - Testing: 12 ok 2 - Testing: 123 ok 3 - Testing: 1234 ok 4 - Testing: 12345 ok 5 - Testing: 123 4567 890 ok 6 - Testing: 123 4567 89 not ok 7 - Testing: 123-4567-890 # Failed test 'Testing: 123-4567-890' # at ./pm_1178784_regex_digits.pl line 29. # got: '>123<>123<->4567<>4567<->890<>890<' # expected: '123-4567-890' not ok 8 - Testing: 01/02/2017 # Failed test 'Testing: 01/02/2017' # at ./pm_1178784_regex_digits.pl line 29. # got: '01/02/>2017<>2017<' # expected: '01/02/2017' not ok 9 - Testing: 2017-01-02T17:01:34 # Failed test 'Testing: 2017-01-02T17:01:34' # at ./pm_1178784_regex_digits.pl line 29. # got: '>2017<>2017<-01-02T17:01:34' # expected: '2017-01-02T17:01:34' ok 10 - Testing: 12 # 345 # 6789 # 0 # Looks like you failed 3 tests of 10.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Recognizing 3 and 4 digit number
by davido (Cardinal) on Jan 03, 2017 at 17:53 UTC |