My approach below. If split could work starting from right to left instead of the other way round, I'd use it! But alas it doesn't do that! Below I worked from right to left and used \S (non-space) and \s (space) Perl short-cuts. I think it is fine to use a combination of "fixing" and regex "splitting". In the case as explained so far, it is certainly possible to do everything in one regex. But, you've already seen the value in changing the tabs to | characters so that you can see them and print. Sometimes these intermediate steps work out to be real handy for debugging!
#!/usr/bin/perl -w use strict; my @tests = ( 'A ||118|AVIANN GILDED WILD HONEY. HM 75081701. 02-04-97', 'A ||118|||AVIANN GILDED &^$WILD HONEY HP--09090901. 02-04-97', ); foreach my $test (@tests) { $test =~ s/^.*\|//; #remove beginning til last | #"fix" possible typo in the registration number # HP--09090901. becomes HP 09090901. $test =~ s/(\w+)[-]+([\d.]+\s+\S+)$/$1 $2/; my ($name,$number,$date) = $test =~ m/^(.*)\s+(\S+\s+\S+)\s+(\S+)$/ +; $number =~s/\.$//; #fix possible typo trailing '.' print "\n name=$name\n number=$number\n date=$date\n"; } __END__ name=AVIANN GILDED WILD HONEY. number=HM 75081701 date=02-04-97 name=AVIANN GILDED &^$WILD HONEY number=HP 09090901 date=02-04-97
In reply to Re: regexp identify variable number of digits within a sentence
by Marshall
in thread regexp identify variable number of digits within a sentence
by bdalzell
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |