|
From: seanjao on 18 Dec 2006 15:21 Hi there: I would like to use Perl to search a file containing data like the following. aaa01234 99.99 99.99 99.99 bb0567 99.99 99.99 99.99 ccccc89101 99.99 99.99 99.99 How can I let perl to match the first column if I type "bb567" without the zero between "bb" and "567"? Thanks Sean
From: Glenn Jackman on 18 Dec 2006 15:53 At 2006-12-18 03:21PM, "seanjao(a)yahoo.com" wrote: > Hi there: > > I would like to use Perl to search a file containing data like the > following. > aaa01234 99.99 99.99 99.99 > bb0567 99.99 99.99 99.99 > ccccc89101 99.99 99.99 99.99 > > How can I let perl to match the first column if I type "bb567" without > the zero between "bb" and "567"? Based on these requirements: my $wanted = 'bb567'; while (<DATA>) { if (/^$wanted\b/) { print 'exact match >> '; } elsif (/^([a-z]+)0+(\d+)/ and $wanted eq "$1$2") { print 'munged match>> '; } else { print 'no match >> '; } print; } __DATA__ aaa01234 99.99 99.99 99.99 bb0567 99.99 99.99 99.99 ccccc89101 99.99 99.99 99.99 -- Glenn Jackman Ulterior Designer
From: Dr.Ruud on 18 Dec 2006 19:00 Glenn Jackman schreef: > if (/^$wanted\b/) { Safer: if (/^\Q$wanted\E\b/) { -- Affijn, Ruud "Gewoon is een tijger."
From: Josef Moellers on 19 Dec 2006 02:47 Glenn Jackman wrote: > At 2006-12-18 03:21PM, "seanjao(a)yahoo.com" wrote: > >> Hi there: >> >> I would like to use Perl to search a file containing data like the >> following. >> aaa01234 99.99 99.99 99.99 >> bb0567 99.99 99.99 99.99 >> ccccc89101 99.99 99.99 99.99 >> >> How can I let perl to match the first column if I type "bb567" without >> the zero between "bb" and "567"? > > > Based on these requirements: > > my $wanted = 'bb567'; > > while (<DATA>) { > if (/^$wanted\b/) { > print 'exact match >> '; > } > elsif (/^([a-z]+)0+(\d+)/ and $wanted eq "$1$2") { Based on the requirements "without *the* zero", that would be elsif (/^([a-z]+)0?(\d+)/ and $wanted eq "$1$2") { Personally, I would have made that elsif (/^([a-z]+)0*(\d+)/ and $wanted eq "$1$2") Josef -- Josef Möllers (Pinguinpfleger bei FSC) If failure had no penalty success would not be a prize -- T. Pratchett
From: seanjao on 19 Dec 2006 10:43
Thanks guys! It helps. Sean |