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

From: John Dunlop (john+usenet_at_johndunlop.info)
Date: 02/13/04


Date: Fri, 13 Feb 2004 06:31:06 -0000

Sims wrote:

> $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?

Because that pattern doesn't allow letters outside quoted strings.

> What part looks for a decimal number?

The decimal number character type ("\d"). That's in the second
alternative, near the end of the pattern.

> 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).

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


Relevant Pages

  • RE: [PHP] Regular expressions
    ... way the letters are arranged? ... *function convertToPattern converts a string into a pattern based on ... $arrHolder = array( ...
    (php.general)
  • Re: ID question: What does "specified complexity" mean?
    ... the alphabet is specified without being complex (i.e., it conforms to ... an independently given pattern but is simple). ...  That is, in order to be "specified", a group of letters ... Dembski then argues that "evolutionary algorithms" (by which he seems ...
    (talk.origins)
  • Re: ID question: What does "specified complexity" mean?
    ... the alphabet is specified without being complex (i.e., it conforms to ... an independently given pattern but is simple). ... That is, in order to be "specified", a group of letters ... Dembski then argues that "evolutionary algorithms" (by which he seems ...
    (talk.origins)
  • Re: ID question: What does "specified complexity" mean?
    ... the alphabet is specified without being complex (i.e., it conforms to ... an independently given pattern but is simple). ... That is, in order to be "specified", a group of letters ... Dembski then argues that "evolutionary algorithms" (by which he seems ...
    (talk.origins)
  • Re: ID question: What does "specified complexity" mean?
    ... the alphabet is specified without being complex (i.e., it conforms to ... an independently given pattern but is simple). ...  That is, in order to be "specified", a group of letters ... Dembski then argues that "evolutionary algorithms" (by which he seems ...
    (talk.origins)