By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

regex/preg_replace? How do I do this?

P: n/a
GoL
Hi all,

I have a problem that I *think* is simple, but since I'm completely
ignorant about regex expression I don't have a clue how to do it...

What I need to do is to find all instances in a string where there are
exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
with a # sign.
So if I have the string:

"some text here 1234567890 more text 0987654321 and more text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more text"

How do I do this?
Greatful for any help.

Thanks,
/G
Jun 4 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
GoL,

You could use something like this:

preg_replace('/(\d{3})\d{3}(\d{4})/', '\1###\2', '1234567890');

where '1234567890' is your text. Depending on the format of your
input source, you may also need to specify the delimiter at the
beginning or end of the expression, but this will work for a plain
list of the 10-digit numbers.

I'd really advise you to read up on regular expressions. Strangely,
the TextMate book (http://www.pragmaticprogrammer.com/titles/
textmate/) really clarified them for me (if you're a mac user).

ethan

On Jun 4, 7:58 am, GoL <usenet@^^.wwwspace.net._nospam_^^wrote:
Hi all,

I have a problem that I *think* is simple, but since I'm completely
ignorant about regex expression I don't have a clue how to do it...

What I need to do is to find all instances in a string where there are
exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
with a # sign.
So if I have the string:

"some text here 1234567890 more text 0987654321 and more text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more text"

How do I do this?
Greatful for any help.

Thanks,
/G
Jun 4 '07 #2

P: n/a
On 04.06.2007 13:58 GoL wrote:
Hi all,

I have a problem that I *think* is simple, but since I'm completely
ignorant about regex expression I don't have a clue how to do it...

What I need to do is to find all instances in a string where there are
exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
with a # sign.
So if I have the string:

"some text here 1234567890 more text 0987654321 and more text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more text"

How do I do this?
Greatful for any help.

Thanks,
/G
maybe

$regexp = '~
(
(?: ^ | \D)
\d{3}
)
\d{3}
(?=
\d{4}
(\D | $)
)
~x';

echo preg_replace($regexp, '$1###', $your_example));
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #3

P: n/a

GoL <usenet@^^.wwwspace.net._nospam_^^wrote in
<j5********************************@4ax.com>:
So if I have the string:

"some text here 1234567890 more text 0987654321 and more
text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more
text"
pavel@debian:~/dev/php$ cat r.php
#!/usr/bin/php
<?php
$str =
'"1111111111 some text here 1234567890, more text ' .
'0987654321 and more text 2222222222"' ;
echo (
preg_replace (
'/([\W^]\d{3})\d{3}(\d{4}[\W$])/' ,
'\\1###\\2' ,
$str ) .
"\n" ) ;
?>
pavel@debian:~/dev/php$ ./r.php
"111###1111 some text here 123###7890, more text 098###4321
and more text 222###2222"
pavel@debian:~/dev/php$

--
Pavel Lepin
Jun 4 '07 #4

P: n/a
On 04.06.2007 14:52 Pavel Lepin wrote:
GoL <usenet@^^.wwwspace.net._nospam_^^wrote in
<j5********************************@4ax.com>:
>So if I have the string:

"some text here 1234567890 more text 0987654321 and more
text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more
text"

pavel@debian:~/dev/php$ cat r.php
#!/usr/bin/php
<?php
$str =
'"1111111111 some text here 1234567890, more text ' .
'0987654321 and more text 2222222222"' ;
echo (
preg_replace (
'/([\W^]\d{3})\d{3}(\d{4}[\W$])/' ,
'\\1###\\2' ,
$str ) .
"\n" ) ;
?>
pavel@debian:~/dev/php$ ./r.php
"111###1111 some text here 123###7890, more text 098###4321
and more text 222###2222"
pavel@debian:~/dev/php$
Pavel, [\W$] doesn't mean "non-word or end of line" as you seem to
think. Metachars don't work in character classes.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #5

P: n/a

gosha bine <st********@gmail.comwrote in
<46**********************@read.cnntp.org>:
On 04.06.2007 14:52 Pavel Lepin wrote:
>GoL <usenet@^^.wwwspace.net._nospam_^^wrote in
<j5********************************@4ax.com>:
>>"some text here 1234567890 more text 0987654321 and more
text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more
text"

<?php
$str =
'"1111111111 some text here 1234567890, more text ' .
'0987654321 and more text 2222222222"' ;
echo (
preg_replace (
'/([\W^]\d{3})\d{3}(\d{4}[\W$])/' ,
'\\1###\\2' ,
$str ) .
"\n" ) ;
?>
pavel@debian:~/dev/php$ ./r.php
"111###1111 some text here 123###7890, more text
098###4321 and more text 222###2222"

Pavel, [\W$] doesn't mean "non-word or end of line" as you
seem to think. Metachars don't work in character classes.
That seems to be the case, indeed. I was baffled as to why
it still seemed to work even despite that, and then
suddenly realized the string I used started and ended with
double-quotes.

--
Pavel Lepin
Jun 4 '07 #6

P: n/a
chowsapal kirjoitti:
On Jun 4, 7:58 am, GoL <usenet@^^.wwwspace.net._nospam_^^wrote:
>Hi all,

I have a problem that I *think* is simple, but since I'm completely
ignorant about regex expression I don't have a clue how to do it...

What I need to do is to find all instances in a string where there are
exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
with a # sign.
So if I have the string:

"some text here 1234567890 more text 0987654321 and more text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more text"

How do I do this?
Greatful for any help.

Thanks,
/G

GoL,

You could use something like this:

preg_replace('/(\d{3})\d{3}(\d{4})/', '\1###\2', '1234567890');

where '1234567890' is your text. Depending on the format of your
input source, you may also need to specify the delimiter at the
beginning or end of the expression, but this will work for a plain
list of the 10-digit numbers.
But it should not match to 123456789012345 (ie. >10 digits) but your
example will match it. When you delimit the beginning and end of the
number to a word boundary, you'd want something like:
/\b(\d{3})\d{3}(\d{4})\b/ right?

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Jun 4 '07 #7

P: n/a
GoL
On Mon, 04 Jun 2007 13:58:37 +0200, GoL
<usenet@^^.wwwspace.net._nospam_^^wrote:
>Hi all,

I have a problem that I *think* is simple, but since I'm completely
ignorant about regex expression I don't have a clue how to do it...

What I need to do is to find all instances in a string where there are
exactly 10 digits in a row, and replace the 4th, 5th and 6th digit
with a # sign.
So if I have the string:

"some text here 1234567890 more text 0987654321 and more text"

I want it replaced with:

"some text here 123###7890 more text 098###4321 and more text"

How do I do this?
Greatful for any help.

Thanks,
/G
Thanks all, I got it solved with the help of your input. :)

/G
Jun 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.