pattern match and extract problem

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

    pattern match and extract problem

    Hi All,
    I expect someone can crack this one in no time.

    Ok, what I'd like to do is to match a pattern in a string and extract a
    portion of it. For example if I had a string like:

    'The store had apples(20) and pears(30)'

    how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)', how
    do I expect the '??'.


    Ta in advance,
    Dave


  • Sean Barton

    #2
    Re: pattern match and extract problem

    easy enough. you can use a string tokeniser and use the ( as the
    delimeter. it depends on how random the string will be to how you will
    harvest the number.

    for example:

    $string = strtok($longstr ing,'(');

    will give you:

    20) and pears

    then from then you can go...

    $num1 = strtok($string, ')');

    $num1 would be 20.

    its basic but would work.now its up to you how you work it you can run
    more than one tokeniser at once so tokenise at the ' ' to get each word
    or look into a function called explode()

    i suppose to say that if the strings will be pretty regular then you
    can use this and if not you would have to make it a little more dynamic


    hope this helps

    Sean Barton

    Comment

    • Allen

      #3
      Re: pattern match and extract problem

      On Tue, 07 Feb 2006 16:05:32 -0500, David Moore
      <dave_m_moore@p ost2me.freeserv e.co.uk> wrote:
      [color=blue]
      > Hi All,
      > I expect someone can crack this one in no time.
      >
      > Ok, what I'd like to do is to match a pattern in a string and extract a
      > portion of it. For example if I had a string like:[/color]

      I prefer using the preg functions to extract parts of strings, but that
      requires at least a basic understanding of regular expressions. At any
      rate, if you want to capture all instances of word(number) you could do
      this:
      preg_match_all( '/[a-z]+\(([0-9]+)\)/im',$string,$ma tches);

      $matches[0] would be an array of apples(20), pears(30) etc.,
      $matches[1] would be an array of all the numbers (20, 30, etc.)

      regex match result -> http://tinyurl.com/9yrmc

      A.

      --

      A Web based regular expressions powered find/replace utility

      Comment

      • Jim Michaels

        #4
        Re: pattern match and extract problem


        "David Moore" <dave_m_moore@p ost2me.freeserv e.co.uk> wrote in message
        news:dsb232$ean $1@news7.svr.po l.co.uk...[color=blue]
        > Hi All,
        > I expect someone can crack this one in no time.
        >
        > Ok, what I'd like to do is to match a pattern in a string and extract a
        > portion of it. For example if I had a string like:
        >
        > 'The store had apples(20) and pears(30)'
        >
        > how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)',
        > how do I expect the '??'.[/color]

        sounds like you only want the numbers. easy.
        preg_match_all( "/\((\d\d)\)/", $string, $matches);
        or if you want to handle any number of digits,
        preg_match_all( "/\((\d+)\)/", $string, $matches);
        [color=blue]
        >
        >
        > Ta in advance,
        > Dave
        >
        >[/color]


        Comment

        Working...