Re: preg_match and delimited strings



siromega@xxxxxxxxx wrote:
I found a regex and a function that accomplished what I
needed...

function csv_string_to_array($str){
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
return preg_replace("/^\"(.*)\"$/","$1",$results);
}
I just had to replace the \" with ' to get it to parse my strings. I
tested it with both '' and , in the encapsulated string and it works!


I hate to be the bearer of bad tidings, but it will fail on single escaped
\":

$str = '"baz,\"bax", bay, foz, "a, b", "foo';
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
$res = preg_replace("/^\"(.*)\"$/","$1",$results);
print_r($res);

Array
(
[0] => "baz
[1] => \"bax"
[2] => bay
[3] => foz
[4] => "a, b"
[5] => foo
)

Properly nested, it can handle it:
$str = '"baz,\"bax\"", bay, foz, "a, b", foo';

Array
(
[0] => baz,\"bax\"
[1] => bay
[2] => foz
[3] => "a, b"
[4] => foo
)

Also a nice one is:
$str = 'bay, foz, "a, b", "fo\"o"';


Array
(
[0] => bay, foz, "a
[1] => b", "fo\"o"
)


Not a very robust one here.
But maybe it's OK for your data, it all depends how much you know for sure
about that.

Grtz,
--
Rik Wasmus


.



Relevant Pages

  • Re: Text Files or binaries with objects?
    ... I have some large text files that I need to repeatedly analyse. ... parse the strings and load the data into my custom object ready to be ... file many times over many sessions, to parse the text files once, then save ...
    (alt.comp.lang.borland-delphi)
  • Text Files or binaries with objects?
    ... I have some large text files that I need to repeatedly analyse. ... parse the strings and load the data into my custom object ready to be ... file many times over many sessions, to parse the text files once, then save ...
    (alt.comp.lang.borland-delphi)
  • Re: Is it possible to have special characters in list
    ... You *have* to parse that line by line! ... While I agree that one can't treat arbitrary strings as a list, ... way to sensibly handle that list, just because the plaintext-column ... targetted to the problem at hand (parsing that particular format), ...
    (comp.lang.tcl)
  • Re: parsing problem
    ... find the most middle operand and the strings between it -> string1: ... I think that what you really want to get in the end here is a parse ... the only three kinds of nodes in the tree ... static Node ParseAOExpr(string input, ref int index) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TextboxColumn Nur Zahlen (numerisch)
    ... keine lokalisierten Strings, ... die zwei Fehlermeldungen mehr wird man ja gerade noch hinbekommen. ... Diese kann man nur bei Parse so identifizieren und respektive behandeln. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)