Path: utzoo!attcan!uunet!know!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Pattern Substitution Question Keywords: pattern Message-ID: <10154@jpl-devvax.JPL.NASA.GOV> Date: 29 Oct 90 20:50:13 GMT References: <1086@dg.dg.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 41 In article <1086@dg.dg.com> blurie@einstein.webo.dg.com writes: : Here's a question, whose answer I'm sure is going to be trivial, that I've : been trying to resolve for days. Given input like the following: : : Lurie;Benjamin;1212 Nowhere Road;(617) 923-0000;(w) (000) 202-2343 : : I would like to translate it to: : : Benjamin Lurie;1212 Nowhere Road;(617) 923-0000;(w) (000) 202-2343 : : I've tried a pattern match looking for a semi-colon, but I always get the : longest pattern that matches, which would include everything up to the "(w)". : : What pattern substitution would accomplish what I want here? And, in general, : how would I specify to match only the input up to the first ocurrance of : , where is some specific character? The general solution is to use a negated character class. For semicolons, say something like: while (<>) { s/^([^;]*);([^;]*);/$2 $1;/; print; } However, if you're looking for readability it might be cleaner to say something like: while (<>) { ($lastname, $firstname, $rest) = split(/;/, $_, 3); print "$firstname $lastname;$rest"; } And, believe it or not, it runs faster, at least on my machine. You can shave another 7-8% off by saying print $firstname, ' ', $lastname, ';', $rest; which avoids some string copying. Larry