Professional Web Applications Themes

preg_match_all optional subpattern - PHP Development

Using preg_match_all, I need to capture a list of first and last names plus an optional country code proceeding them. For example: <tr><td>AU</td><td>Jane Smith</td></tr> <tr><td></td><td>Bill Johnson</td></tr> <tr><td>GB</td><td>Larry Brown</td></tr> <tr><td>US</td><td>Mary Jordon</td></tr> <tr><td></td><td>Peter Jones</td></tr> The country code might exist, it might not. I would like the array contents to look like this: AU Jane Smith Bill Johnson US Larry Brown GB Mary Jordon Peter Jones I know a subpattern is needed of all the possible country codes: AU|GB|US but how do you include this as an optional subpattern? Thanks in advance....

  1. #1

    Default preg_match_all optional subpattern

    Using preg_match_all, I need to capture a list of first and last names plus
    an optional country code proceeding them.

    For example:

    <tr><td>AU</td><td>Jane Smith</td></tr>
    <tr><td></td><td>Bill Johnson</td></tr>
    <tr><td>GB</td><td>Larry Brown</td></tr>
    <tr><td>US</td><td>Mary Jordon</td></tr>
    <tr><td></td><td>Peter Jones</td></tr>

    The country code might exist, it might not.

    I would like the array contents to look like this:

    AU Jane Smith
    Bill Johnson
    US Larry Brown
    GB Mary Jordon
    Peter Jones

    I know a subpattern is needed of all the possible country codes:

    AU|GB|US

    but how do you include this as an optional subpattern?

    Thanks in advance.






    Han Guest

  2. #2

    Default Re: preg_match_all optional subpattern

    Han wrote:
    > Using preg_match_all, I need to capture a list of first and last names plus
    > an optional country code proceeding them.
    >
    > For example:
    >
    > <tr><td>AU</td><td>Jane Smith</td></tr>
    > <tr><td></td><td>Bill Johnson</td></tr>
    > <tr><td>GB</td><td>Larry Brown</td></tr>
    > <tr><td>US</td><td>Mary Jordon</td></tr>
    > <tr><td></td><td>Peter Jones</td></tr>
    >
    > [...] I know a subpattern is needed of all the possible country codes:
    >
    > AU|GB|US
    >
    > but how do you include this as an optional subpattern?
    The ? quantifier means zero or one of whatever came before,
    representable by {0,1}. Quantifying a subpattern using the
    question mark denotes its nonobligatory nature.

    So, to match optional two-letter country codes within a table cell
    (doesn't properly cater for attributes, but that's rectifiable):

    `<td.*>([a-z]{2})?</td.*>`Usi

    If you wish to list the possible values, precluding others:

    `<td.*>(au|gb|us)?</td.*>`Usi

    --
    Jock
    John Dunlop Guest

  3. #3

    Default Re: preg_match_all optional subpattern

    "Han" <nobodynowhere.com> wrote in message news:<TT4gb.225533$mp.141550rwcrnsc51.ops.asp.att .net>...
    > Using preg_match_all, I need to capture a list of first and last names plus
    > an optional country code proceeding them.
    >
    > For example:
    >
    > <tr><td>AU</td><td>Jane Smith</td></tr>
    > <tr><td></td><td>Bill Johnson</td></tr>
    > <tr><td>GB</td><td>Larry Brown</td></tr>
    > <tr><td>US</td><td>Mary Jordon</td></tr>
    > <tr><td></td><td>Peter Jones</td></tr>
    >
    > The country code might exist, it might not.
    >
    > I would like the array contents to look like this:
    >
    > AU Jane Smith
    > Bill Johnson
    > US Larry Brown
    > GB Mary Jordon
    > Peter Jones
    >
    > I know a subpattern is needed of all the possible country codes:
    >
    > AU|GB|US
    this pattern should do the job:

    "{<tr><td>\s*([A-Z]{2})?\s*</td><td>\s*(\w+)?\s*(\w+)\s*</td></tr>}im"

    if this pattern is used in preg_match_all, it should produce the
    desired result.
    it will extract the country code if availible, first name if
    availible, and last name.
    They will be put in an 2 dim array. If no country code or first name
    is given, the array element will be left empty.

    hope this helps,

    sascha


    >
    > but how do you include this as an optional subpattern?
    >
    > Thanks in advance.
    s van gemmert Guest

  4. #4

    Default Re: preg_match_all optional subpattern

    John,

    Thank you for another detailed reply.

    The cryptic syntax is beginning to slowly sink in, but there's still a few
    nagging issues.

    In my price list, the amount may or may not be preceded with a $ sign.

    For instance, the list might look like this:

    $2.99
    1.99
    $3.00
    $4.00

    I modified my price pattern to accommodate this:

    ((\\$|\s*)?\d{1,3}\.\d{2})

    which works great. The problem is, it also creates another array dimension
    that contains only $ or space:

    $

    $
    $

    I can simply ignore this dimension, but is there a way to prevent it?

    Thanks (again) in advance.

    "John Dunlop" <john+usenetjohndunlop.info> wrote in message
    news:MPG.19eb262ef3b81717989777news.freeserve.net ...
    > Han wrote:
    >
    > > Using preg_match_all, I need to capture a list of first and last names
    plus
    > > an optional country code proceeding them.
    > >
    > > For example:
    > >
    > > <tr><td>AU</td><td>Jane Smith</td></tr>
    > > <tr><td></td><td>Bill Johnson</td></tr>
    > > <tr><td>GB</td><td>Larry Brown</td></tr>
    > > <tr><td>US</td><td>Mary Jordon</td></tr>
    > > <tr><td></td><td>Peter Jones</td></tr>
    > >
    > > [...] I know a subpattern is needed of all the possible country codes:
    > >
    > > AU|GB|US
    > >
    > > but how do you include this as an optional subpattern?
    >
    > The ? quantifier means zero or one of whatever came before,
    > representable by {0,1}. Quantifying a subpattern using the
    > question mark denotes its nonobligatory nature.
    >
    > So, to match optional two-letter country codes within a table cell
    > (doesn't properly cater for attributes, but that's rectifiable):
    >
    > `<td.*>([a-z]{2})?</td.*>`Usi
    >
    > If you wish to list the possible values, precluding others:
    >
    > `<td.*>(au|gb|us)?</td.*>`Usi
    >
    > --
    > Jock


    Han Guest

  5. #5

    Default Re: preg_match_all optional subpattern

    Han wrote:
    > ((\\$|\s*)?\d{1,3}\.\d{2})
    >
    > which works great. The problem is, it also creates another array
    > dimension that contains only $ or space:
    >
    > [...] I can simply ignore this dimension, but is there a way to
    > prevent it?
    Subpatterns that begin with the two character sequence "?:" aren't
    captured. You could then write your pattern as:

    `(?:\\$|\s*)?\d{1,3}\.\d{2}`

    --
    Jock
    John Dunlop Guest

  6. #6

    Default Re: preg_match_all optional subpattern

    Jock,

    That's it--thanks.

    I've been spending some time re-reading the pattern doentation on php.net
    and it's beginning to sink in.

    Again, much appreciated!

    "John Dunlop" <john+usenetjohndunlop.info> wrote in message
    news:MPG.19ec77cdbce17be098977cnews.freeserve.net ...
    > Han wrote:
    >
    > > ((\\$|\s*)?\d{1,3}\.\d{2})
    > >
    > > which works great. The problem is, it also creates another array
    > > dimension that contains only $ or space:
    > >
    > > [...] I can simply ignore this dimension, but is there a way to
    > > prevent it?
    >
    > Subpatterns that begin with the two character sequence "?:" aren't
    > captured. You could then write your pattern as:
    >
    > `(?:\\$|\s*)?\d{1,3}\.\d{2}`
    >
    > --
    > Jock

    Han Guest

Similar Threads

  1. numberformat - is optional no longer optional?
    By miki in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 18th, 11:07 PM
  2. preg_match_all total newbie
    By Tim Van Wassenhove in forum PHP Development
    Replies: 1
    Last Post: January 18th, 02:02 AM
  3. preg_match exclude string within subpattern
    By Han in forum PHP Development
    Replies: 3
    Last Post: October 7th, 08:38 PM
  4. preg_match VS preg_match_all
    By Han in forum PHP Development
    Replies: 2
    Last Post: October 4th, 09:21 PM
  5. [PHP] preg_match_all
    By Curt Zirzow in forum PHP Development
    Replies: 0
    Last Post: September 23rd, 06:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139