One liner for extracting a number

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

    One liner for extracting a number

    Hello,
    Can regular expressions help me here? I want to extract a number
    from a string which will always be of the form "parameter# ##" where
    "###" is an arbitrary number of numeric digits. So if my string
    contained

    parameter24
    parameter8
    parameter90210

    I would want to extract "24", "8", and "90210" respectively.

    Thanks for any help, -
  • kingofkolt

    #2
    Re: One liner for extracting a number

    "D. Alvarado" <laredotornado@ zipmail.com> wrote in message
    news:9fe1f2ad.0 408141900.21ebd a81@posting.goo gle.com...[color=blue]
    > Hello,
    > Can regular expressions help me here? I want to extract a number
    > from a string which will always be of the form "parameter# ##" where
    > "###" is an arbitrary number of numeric digits. So if my string
    > contained
    >
    > parameter24
    > parameter8
    > parameter90210
    >
    > I would want to extract "24", "8", and "90210" respectively.
    >
    > Thanks for any help, -[/color]

    If the string will always contain only "parameter# ##" and nothing before or
    after "parameter# ##", then this should work:

    $str = "parameter2 4";
    preg_match( "/parameter(\d+)/", $str, $matches );
    print_r( $matches );

    /* OUTPUT:

    Array
    (
    [0] => parameter24
    [1] => 24
    )

    */


    Comment

    • Chris Hope

      #3
      Re: One liner for extracting a number

      D. Alvarado wrote:
      [color=blue]
      > Hello,
      > Can regular expressions help me here? I want to extract a number
      > from a string which will always be of the form "parameter# ##" where
      > "###" is an arbitrary number of numeric digits. So if my string
      > contained
      >
      > parameter24
      > parameter8
      > parameter90210
      >
      > I would want to extract "24", "8", and "90210" respectively.[/color]

      In this example $string is your string like "parameter90210 ". $matches is an
      array containing the matches where $matches[1] is the stuff matched
      inbetween ()

      ereg("parameter ([0-9]*)", $string, $matches);

      --
      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • Ian.H

        #4
        Re: One liner for extracting a number

        On Sat, 14 Aug 2004 20:00:56 -0700, D. Alvarado wrote:
        [color=blue]
        > Hello,
        > Can regular expressions help me here? I want to extract a number
        > from a string which will always be of the form "parameter# ##" where
        > "###" is an arbitrary number of numeric digits. So if my string
        > contained
        >
        > parameter24
        > parameter8
        > parameter90210
        >
        > I would want to extract "24", "8", and "90210" respectively.
        >
        > Thanks for any help, -[/color]


        preg_match('/([0-9]+)$/', $your_string, $matches);
        $digits = $matches[1];



        Regards,

        Ian

        Note: preg_* is faster and much more flexible than ereg* functions =)

        --
        Ian.H
        digiServ Network
        London, UK


        Comment

        • Chung Leong

          #5
          Re: One liner for extracting a number

          "D. Alvarado" <laredotornado@ zipmail.com> wrote in message
          news:9fe1f2ad.0 408141900.21ebd a81@posting.goo gle.com...[color=blue]
          > Hello,
          > Can regular expressions help me here? I want to extract a number
          > from a string which will always be of the form "parameter# ##" where
          > "###" is an arbitrary number of numeric digits. So if my string
          > contained
          >
          > parameter24
          > parameter8
          > parameter90210
          >
          > I would want to extract "24", "8", and "90210" respectively.
          >
          > Thanks for any help, -[/color]

          Well, if it will always be "parameter# ##", then you can simply trim off the
          first 9 characters of the text.

          $num = (int) substr($s, 9);


          Comment

          • Christopher Finke

            #6
            Re: One liner for extracting a number

            "D. Alvarado" <laredotornado@ zipmail.com> wrote in message
            news:9fe1f2ad.0 408141900.21ebd a81@posting.goo gle.com...[color=blue]
            > Can regular expressions help me here? I want to extract a number
            > from a string which will always be of the form "parameter# ##" where
            > "###" is an arbitrary number of numeric digits. So if my string
            > contained
            >
            > parameter24
            > parameter8
            > parameter90210
            >
            > I would want to extract "24", "8", and "90210" respectively.[/color]

            I can't believe no one has mentioned this:

            $string = "parameter1234" ;
            $number = str_replace("pa rameter","",$st ring);

            Chris Finke


            Comment

            • Gary L. Burnore

              #7
              Re: One liner for extracting a number

              On Sun, 15 Aug 2004 01:17:43 -0500, "Christophe r Finke"
              <chris@efinke.c om> wrote:
              [color=blue]
              >"D. Alvarado" <laredotornado@ zipmail.com> wrote in message
              >news:9fe1f2ad. 0408141900.21eb da81@posting.go ogle.com...[color=green]
              >> Can regular expressions help me here? I want to extract a number
              >> from a string which will always be of the form "parameter# ##" where
              >> "###" is an arbitrary number of numeric digits. So if my string
              >> contained
              >>
              >> parameter24
              >> parameter8
              >> parameter90210
              >>
              >> I would want to extract "24", "8", and "90210" respectively.[/color]
              >
              >I can't believe no one has mentioned this:
              >
              >$string = "parameter1234" ;
              >$number = str_replace("pa rameter","",$st ring);[/color]

              If I would have seen his post before yours, I would have. :)

              Wonder why his isn't in the spool. Hmmmmm...... Better go look.


              --
              gburnore@databa six dot com
              ---------------------------------------------------------------------------
              How you look depends on where you go.
              ---------------------------------------------------------------------------
              Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
              Black Helicopter Repair Svcs Division | Official Proof of Purchase
              =============== =============== =============== =============== ===============
              Want one? GET one! http://signup.databasix.com
              =============== =============== =============== =============== ===============

              Comment

              • D. Alvarado

                #8
                Re: One liner for extracting a number

                These are all excellent solutions. You guys rock!

                "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<ZZKdndAbo OQLYIPcRVn-qw@comcast.com> ...[color=blue]
                > "D. Alvarado" <laredotornado@ zipmail.com> wrote in message
                > news:9fe1f2ad.0 408141900.21ebd a81@posting.goo gle.com...[color=green]
                > > Hello,
                > > Can regular expressions help me here? I want to extract a number
                > > from a string which will always be of the form "parameter# ##" where
                > > "###" is an arbitrary number of numeric digits. So if my string
                > > contained
                > >
                > > parameter24
                > > parameter8
                > > parameter90210
                > >
                > > I would want to extract "24", "8", and "90210" respectively.
                > >
                > > Thanks for any help, -[/color]
                >
                > Well, if it will always be "parameter# ##", then you can simply trim off the
                > first 9 characters of the text.
                >
                > $num = (int) substr($s, 9);[/color]

                Comment

                Working...