Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

"Its Late & I Cant Spot The Error
Welcome, Guest. Please login or register.
January 09, 2009, 06:36:39 AM
11313 Posts in 1251 Topics by 508 Members
Latest Member: pissematbox
Experts Round Table Network  |  Serverside Technology  |  PHP  |  "Its Late & I Cant Spot The Error « previous next »
Pages: [1]
Author Topic: "Its Late & I Cant Spot The Error  (Read 431 times)
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« on: April 13, 2007, 05:28:01 AM »

Quote
<?php 

preg_match
("/\x5C([^\x5C]+)\.txt\n(\d{2,3})\n([\d:]{8})/"$data$match);

?>

is causing the following error "Warning: preg_match(): Compilation failed: unmatched parentheses at offset 33"
But I seem to be blind and can't find whats wrong with it.
« Last Edit: April 13, 2007, 05:29:46 AM by CrYpTiC_MauleR » Logged

[x] Fight | www.crypticmauler.com
"You must be
GrandSchtroumpf
Mentor

Offline Offline

Posts: 411



« Reply #1 on: April 13, 2007, 07:12:52 AM »

how about changing the double quotes to single quotes... or using double escaping?
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #2 on: April 13, 2007, 11:33:24 AM »

double quotes should work though, also double escaping and just using hex value should be the same. Why does it errors about an unclosed parentheses...weird. I'll try your suggestion, but if it works I will be even more confused.

preg_match('/\x5C([^\x5C]+)\.mp3\n(\d{2,3})\n([\d:]{8})/', $data, $match);

that worked, but why?
« Last Edit: April 13, 2007, 11:39:15 AM by CrYpTiC_MauleR » Logged

[x] Fight | www.crypticmauler.com
"You must be
GrandSchtroumpf
Mentor

Offline Offline

Posts: 411



« Reply #3 on: April 17, 2007, 02:42:35 AM »

It's only a matter of correctly escaping your characters and correctly using PHP's automatic string evaluation (string declarations with 'double quotes')...
I always use single quotes when defining regexps, it's much safer.


Here is a little test i wrote to illustrate the differences:
Code
Language: php (GeSHi-highlighted)
<?php
 
$data = '
\\dummy.txt
664
05:42:18
'
;
 
echo "<pre>
 
 
"
;
 
echo "data = $data <br>";
 
 
Echo '
<h2>PATTERN 1</h2>'
;
 
 
$pattern = '/\\\\([^\\\\]+)\\.txt\\n(\\d{2,3})\\n([\\d:]{8})/';
 
echo "\n\n<b>pattern 1 = $pattern</b>";
preg_match($pattern, $data, $match);
echo "\n\nmatch = ". $match[0];
echo "<blockquote>
This is the pattern with all backslashes escaped, to remove all ambiguities.
In this case, we can replace the 'single quotes' with 'double quotes' and the string remains identical.
To match a backslash character, the pattern uses an escaped backslash.
This means quadruple backslash in the string declaration.
Here, the pattern uses 'backslash-n' to match a new line.
</blockquote>"
;
 
 
$pattern = "/\x5C\x5C([^\x5C\x5C]+)\.txt\x5Cn(\x5Cd{2,3})\x5Cn([\x5Cd:]{8})/";
 
echo "\n\n<b>pattern 1 = $pattern</b>";
preg_match($pattern, $data, $match);
echo "\n\nmatch = ". $match[0];
echo "<blockquote>
This is exactly the same pattern but using 'double quotes' and 'hex values' in the string declaration.
</blockquote>
"
;
 
 
Echo '
<h2>PATTERN 2</h2>'
;
 
 
$pattern = '/\x5C([^\x5C]+)\\.txt\\n(\\d{2,3})\\n([\\d:]{8})/';
 
echo "\n\n<b>pattern 2 = $pattern</b>";
preg_match($pattern, $data, $match);
echo "\n\nmatch = ". $match[0];
echo "<blockquote>
This is a different pattern.
It uses the 'hex value' to match the backslash character instead of using an escaped backslash.
</blockquote>"
;
 
 
Echo '
<h2>PATTERN 3</h2>'
;
 
$pattern = "/\\\\([^\\\\]+)\\.txt\n(\\d{2,3})\n([\\d:]{8})/";
 
echo "\n\n<b>pattern 3 = $pattern</b>";
preg_match($pattern, $data, $match);
echo "\n\nmatch = ". $match[0];
echo "<blockquote>
This is yet a different pattern.
It uses the 'line-feed' character to match a new line instead of using a 'backslash-n'.
</blockquote>"
;
 
 
echo "
 
 
</pre>"
;
 
?>


Here is a summary of the code here-above:
Code
Language: php (GeSHi-highlighted)
$pattern = '/\\\\([^\\\\]+)\\.txt\\n(\\d{2,3})\\n([\\d:]{8})/';
$pattern = "/\x5C\x5C([^\x5C\x5C]+)\.txt\x5Cn(\x5Cd{2,3})\x5Cn([\x5Cd:]{8})/";
$pattern = '/\x5C([^\x5C]+)\\.txt\\n(\\d{2,3})\\n([\\d:]{8})/';
$pattern = "/\\\\([^\\\\]+)\\.txt\n(\\d{2,3})\n([\\d:]{8})/";
Logged
VGR
Mentor

Offline Offline

Posts: 684



WWW
« Reply #4 on: April 17, 2007, 06:26:34 AM »

arf arf arf regexps :D
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
GrandSchtroumpf
Mentor

Offline Offline

Posts: 411



« Reply #5 on: April 17, 2007, 07:59:10 AM »

Actually, to be consistant, i should have escaped all the backslashes in the third pattern declaration:
Code
Language: php (GeSHi-highlighted)
$pattern = '/\\x5C([^\\x5C]+)\\.txt\\n(\\d{2,3})\\n([\\d:]{8})/';
This way it should not matter if we use single or double quotes.

Regexps are great... when used appropriately.

IMHO, the most confusing part is how PHP automatically escapes backslashes.  Java does not do that, so you end up with a lot more backslashes in you string declarations, but at least you make less mistakes.
Logged
VGR
Mentor

Offline Offline

Posts: 684



WWW
« Reply #6 on: April 17, 2007, 08:38:53 AM »

il y a deux options magic_quotes dans php.ini
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: