regex/preg_replace? How do I do this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GoL

    regex/preg_replace? How do I do this?

    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
  • chowsapal

    #2
    Re: regex/preg_replace? How do I do this?

    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@^^.wwws pace.net._nospa m_^^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

    Comment

    • gosha bine

      #3
      Re: regex/preg_replace? How do I do this?

      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($r egexp, '$1###', $your_example)) ;


      --
      gosha bine

      extended php parser ~ http://code.google.com/p/pihipi
      blok ~ http://www.tagarga.com/blok

      Comment

      • Pavel Lepin

        #4
        Re: regex/preg_replace? How do I do this?


        GoL <usenet@^^.wwws pace.net._nospa m_^^wrote in
        <j5v763posshgst kulnj9rbvgfoabm h14qf@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

        Comment

        • gosha bine

          #5
          Re: regex/preg_replace? How do I do this?

          On 04.06.2007 14:52 Pavel Lepin wrote:
          GoL <usenet@^^.wwws pace.net._nospa m_^^wrote in
          <j5v763posshgst kulnj9rbvgfoabm h14qf@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

          Comment

          • Pavel Lepin

            #6
            Re: regex/preg_replace? How do I do this?


            gosha bine <stereofrog@gma il.comwrote in
            <46640ea3$0$289 2$6e1ede2f@read .cnntp.org>:
            On 04.06.2007 14:52 Pavel Lepin wrote:
            >GoL <usenet@^^.wwws pace.net._nospa m_^^wrote in
            ><j5v763posshgs tkulnj9rbvgfoab mh14qf@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

            Comment

            • Rami Elomaa

              #7
              Re: regex/preg_replace? How do I do this?

              chowsapal kirjoitti:
              On Jun 4, 7:58 am, GoL <usenet@^^.wwws pace.net._nospa m_^^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?

              --
              Rami.Elomaa@gma il.com

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

              Comment

              • GoL

                #8
                Re: regex/preg_replace? How do I do this?

                On Mon, 04 Jun 2007 13:58:37 +0200, GoL
                <usenet@^^.wwws pace.net._nospa m_^^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


                Comment

                Working...