Regular Expressions

HI,
I have a regular expression which works well as GREP in BBEdit, but I can’t get the thing to work in PHP. When I run this:

$myData = “Item Name,W-0194,toy,W-0194-toy,1.75,0.80\r”;
$pattern = “#(.*?),(.*?),(.*?),(.*?),(.*?),(.*?)\r#”;
$replacement = “INSERT INTO fiesta_spares ( part_name, product_code, part_description, part_no , price , stockLevel ) VALUES ( ‘\1’, ‘\2’, ‘\3’, ‘\4’ , ‘\5’ , ‘100’ );\r”;

$myData = ereg_replace($pattern,$replacement,$myData);
echo $myData;

$myData is unchanged. The escaped characters in $pattern are not in the GREP in BBEdit (unescaped, the thing fails with an error).

I hate regular expressions…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sometime around 22/5/08 (at 05:47 -0400) Paul said:

I hate regular expressions…

Regex: Powerful. Painful. Makes my head hurt.

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Arrggggg… and the forum code has stripped out all the escape characters from the example I posted!!

That noise - hear it? It’s me. Grinding my teeth. again.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sorry about the backslashing, use Pastie for this, I really will work on this as soon as I get a chance. It’s very frustrating, because the live preview gets it right, and the same function is used in both cases. I suspect it has something to do with magic_quotes_gpc, which is just a little too voodoo in some cases.

Also, ereg_replace is not as Perl-compatible as preg_replace – if something works in BBEdit, and doesn’t in ereg, then try preg. Note that you have to be very Perl-y with your regex string in preg, ereg is looser.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Paul, looking at your input string, it would appear that you are trying to parse a CSV file with a regular expression. Could you not do this to cast it into an array?

$properties = preg_split('/,/',$inputString,-1,PREG_SPLIT_NO_EMPTY);

And then blast the fields into your query like this:

$sql = 'INSERT INTO fiesta_spares ( part_name, 
    product_code, 
    part_description, 
    part_no, 
    price, 
    stockLevel ) 
    VALUES ( "' . implode('","',$properties) . '")';

Unless your data fields can contain commas (can’t tell from your example code) then this should just work. Much less mind-bending than trying to grab the indexed properties one at a time inside a regex.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yeah it looks like you are doing straight replacement rather than
looking for specific patterns, I agree with Walter that tokenizing the
string and then replacing the nth token may be the best option (if
that’s what you are wanting to do).

Joe

On 22 May 2008, at 10:47, Paul wrote:

HI,
I have a regular expression which works well as GREP in BBEdit, but
I can’t get the thing to work in PHP. When I run this:

$myData = “Item Name,W-0194,toy,W-0194-toy,1.75,0.80r”;
$pattern = “#(.?),(.?),(.?),(.?),(.?),(.?)r#”;
$replacement = “INSERT INTO fiesta_spares ( part_name, product_code,
part_description, part_no , price , stockLevel ) VALUES ( ‘1’, ’
2’, ‘3’, ‘4’ , ‘5’ , ‘100’ );r”;

$myData = ereg_replace($pattern,$replacement,$myData);
echo $myData;

$myData is unchanged. The escaped characters in $pattern are not in
the GREP in BBEdit (unescaped, the thing fails with an error).

I hate regular expressions…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

HI,
thanks - that seems to have pushed me in the right direction.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options