tags:

views:

110

answers:

1

I'm not very good with regex and i've been kinda scratching my head on this one. I got the following php code using preg_match which is supposed to match all characters in the registry pathing except for the record number... which in this case is "record??]":

<?php 
$reg_section = "[HKEY_LOCAL_MACHIN\SOFTWARE\INTERSTAR TECHNOLOGIES\XMEDIUS\CONFIG MANAGER\SYSTEM\COMPANIES\RECORD11]";

$pattern = "/^(\[HKEY_LOCAL_MACHIN\\\SOFTWARE\\\INTERSTAR TECHNOLOGIES\\\XMEDIUS\\\CONFIG MANAGER\\\SYSTEM\\\COMPANIES\\\RECORD(\d+)\])$/";
if ( preg_match($pattern, $reg_section )) { 

echo "Found"; 
} else { 

echo "not found"; 
}

?>

The error I get is:

Parse error: syntax error, unexpected T_IF in C:\Inetpub\wwwroot\xfax\regmatch.php on line 5

thanks in advance

+3  A: 

You have to escape the [ too:

/^\[HKEY_LOCAL_MACHIN\\SOFTWARE\\INTERSTAR TECHNOLOGIES\\XMEDIUS\\CONFIG MANAGER\\SYSTEM\\COMPANIES\\RECORD(\d+)]$/

Otherwise it marks the begin of a character class.

This works:

$subject = "[HKEY_LOCAL_MACHIN\\SOFTWARE\\INTERSTAR TECHNOLOGIES\\XMEDIUS\\CONFIG MANAGER\\SYSTEM\\COMPANIES\\RECORD11]";
$pattern = "/^\\[HKEY_LOCAL_MACHIN\\\\SOFTWARE\\\\INTERSTAR TECHNOLOGIES\\\\XMEDIUS\\\\CONFIG MANAGER\\\\SYSTEM\\\\COMPANIES\\\\RECORD\\d+\\]$/";
var_dump(preg_match($pattern, $subject));
Gumbo
For the sake of good order, "]" should be escaped as well.
Tomalak
I've ensured both are escaped but i'm still coming up with a parse error
phill
You have no slash on the end, in your original. Did you modify your original to add an escape, or copy-paste Gumbo's?
Chad Birch
i have added the error
phill
i have updated it with the new suggestions.. thanks
phill
There is no semicolon on the end of the $pattern line.
Chad Birch
I think you need to escape the backslashes in PHP: "/^\\[HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\INTERSTAR TECHNOLOGIES\\\\XMEDIUS\\\\CONFIG MANAGER\\\\SYSTEM\\\\COMPANIES\\\\RECORD(\\d+)\\]$/"
Tomalak
You forgot a semicolon.
Gumbo
I forgot to add one more parenthesis.. that made the error go away but its reading "not found" so i'm guessing there is still something wrong with the regular expression pattern.
phill
There’s a semicolon missing at the end of the statement that declared the pattern (right before the `if`).
Gumbo
@Tomalak : Do I need to have double (\\) to escape 1 slash (\)? or is it triple for 1 slash?
phill
@Gumbo: Can you confirm my escaping idea? I'm always in doubt with PHP, but it seems logical to me. But if it is wrong I'd like to delete it so doubt does not spread. ;-)
Tomalak
@phil: Don't change your question based on something I'm not sure of.
Tomalak
It’s better to escape them correctly. So “foo\bar” should be either written as "foo\\bar" or 'foo\bar' (see <http://docs.php.net/manual/en/language.types.string.php#language.types.string.syntax.double>).
Gumbo
No worries.. after I changed the slashes, it now shows found. thanks for all your help!
phill
@Gumbo: See... I was right, and I wasn't, at the same time. That's one of the reasons why PHP makes my brain hurt.
Tomalak