Thank you very much Japhy for this solution

now how can I replace digits with formatted Xs like :

123412341234 to

XXXX-XXXX-1234

and 0123412341234 to

XXXX-XXXX-X1234

using regular expressions.

Thanks in Advance

Pradeep





-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 04, 2002 3:55 PM
To: Pradeep Sethi
Cc: [EMAIL PROTECTED]
Subject: Re: replacing last 4 digits


On Mar 4, Sethi, Pradeep said:

>I have a number 342389842452.
>
>how do a substitute of everything with X but last 4 digits using regular
>expressions
>
>like xxxxxxxx2452

You could take an approach like:

  s/\d(?=\d{4})/x/g;

The (?=...) means "look ahead for ...".  So this regex matches a digit,
and then checks to see if it CAN match four more digits, without really
going there in the string.  If it matches, it replaces the digit with an
"x".

A much simpler approach to understand is:

  s/(\d+)(\d{4})/"x" x length($1) . $2/e;

This time, we match all the digits we can to $1, allowing us to back up a
little to match four digits into $2.  Then we replace ALL of that with
length($1) x's, followed by the four digits saved in $2.  The /e modifier
means that the replacement is code to be executed.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to