Regular expression, (preg_split etc...), some help please.

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

    #16
    Re: Regular expression, (preg_split etc...), some help please.

    "Sims" <siminfrance@ho tmail.com> wrote in message news:<c0gtip$17 dv70$1@ID-162430.news.uni-berlin.de>...[color=blue]
    >
    > $string = 'a1a ,2b , 3c, " 4, \"aaa, 5", 7';
    > preg_match_all( '`(?<=").*?(?=( ?<!\\\)")|\d+`s ', $string, $array);
    > print_r( $array );
    >
    > i get an output like...
    >
    > Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4, \"aaa, 5 [4]
    > => 7 ) )
    >
    > Why are some of the letters ignored?
    >[color=green]
    > > Describing it at a high-ish level, the pattern looks for either of
    > > two alternatives: zero or more quoted substrings, or one or more
    > > decimal digits. A quoted substring, intuitively, begins and ends
    > > with double-quotes.[/color]
    >
    > What part looks for a decimal number?
    > I am afraid that my description should not have included numbers only.
    > I want the RegEx to work for anything.
    >
    > so that a case like
    >
    > $string = 'xx,xx , xx," x, \"x, x", " x, x", xx, "xx", x';
    >
    > would work regardless what 'x' represents a letter, a number or a symbol,
    > (apart form x= " itself).
    >[/color]

    Modify your RegExp according to following example.
    I hope now you will get the expected result.

    [SNIP]

    $str = '1," 2 ,\" 3aaa", 4, 5';
    $arr = preg_match_all( '#(?<=")\s*[\d]+.*[^\\\](?=")|[\d]+#U',$str,$matc hes);
    print_r($matche s);

    [/SNIP]

    --
    Cheers,
    Rahul Anand

    Comment

    • John Dunlop

      #17
      Re: Regular expression, (preg_split etc...), some help please.

      Sims wrote:
      [color=blue]
      > $string = 'a1a ,2b , 3c, " 4, \"aaa, 5", 7';
      > preg_match_all( '`(?<=").*?(?=( ?<!\\\)")|\d+`s ', $string, $array);
      > print_r( $array );
      >
      > i get an output like...
      >
      > Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4, \"aaa, 5 [4]
      > => 7 ) )
      >
      > Why are some of the letters ignored?[/color]

      Because that pattern doesn't allow letters outside quoted strings.
      [color=blue]
      > What part looks for a decimal number?[/color]

      The decimal number character type ("\d"). That's in the second
      alternative, near the end of the pattern.
      [color=blue]
      > I am afraid that my description should not have included numbers only.
      > I want the RegEx to work for anything.
      >
      > so that a case like
      >
      > $string = 'xx,xx , xx," x, \"x, x", " x, x", xx, "xx", x';
      >
      > would work regardless what 'x' represents a letter, a number or a symbol,
      > (apart form x= " itself).[/color]

      Well, I spent some considerable time on this and discovered I was
      going round and round in circles, covering the same ground. I can't
      think of how to do it all in a single regular expression. :-(

      Because the pattern before used assertions to check for double-
      quotes, it would match the commas between quoted substrings. Since
      the assertion is zero-width -- that is, it doesn't consume any
      characters -- the closing double-quote is taken to also mean the
      start of a quoted substring.

      Consider:

      preg_match_all(
      '`"(.*?)(?<!\\\ )"|([^\s,"]+)`s',
      $string,
      $array)

      Matched against your example, this returns three arrays. You can
      retrieve the information you want from the second and third arrays.
      The first array contains all the data, but with double-quotes left
      in. You could use the first array instead, and remove any leading
      and trailing double-quotes.

      --
      Jock

      Comment

      Working...