Re: Regular expression, (preg_split etc...), some help please.
From: John Dunlop (john+usenet_at_johndunlop.info)
Date: 02/13/04
- Next message: Tom Thackrey: "Re: PHP/Oracle SQL statements, OciParse"
- Previous message: Rahul Anand: "Re: Regular expression, (preg_split etc...), some help please."
- In reply to: Sims: "Re: Regular expression, (preg_split etc...), some help please."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Tom Thackrey: "Re: PHP/Oracle SQL statements, OciParse"
- Previous message: Rahul Anand: "Re: Regular expression, (preg_split etc...), some help please."
- In reply to: Sims: "Re: Regular expression, (preg_split etc...), some help please."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|