CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [Laurent_R] HELP!!Regex to match boundary from a range and substitute characters: Edit Log



BillKSmith
Veteran

Sep 17, 2012, 8:14 PM


Views: 4751
Re: [Laurent_R] HELP!!Regex to match boundary from a range and substitute characters

Replace a leading 'L' with a 'Z' only if it is followed by any character in the range [a-i]. (Refer to documentation for zero-width positive lookahead assertion.) Print successful substitutions.

Code
use strict; 
use warnings;
my @phone = <DATA>;
print grep {s/^L(?=[a-i])/Z/} @phone;
__DATA__
Joshua Smith
John Jenkins
Larry Roberts
Bob Jones
Linda Green
Tim Anderson
Adam Jackso


Consider using a one-liner.

Code
perl -pe"$_=q() unless s/^L(?=[a-i])/Z/" names.dat

Note: Quoting is for windows. You may have to replace double quotes with single quotes.
Good Luck,
Bill

(This post was edited by BillKSmith on Sep 17, 2012, 8:33 PM)


Edit Log:
Post edited by BillKSmith (Veteran) on Sep 17, 2012, 8:33 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives