Delimiter must not be alphanumeric or backslash

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Delimiter must not be alphanumeric or backslash

Post by Cirdan »

So I'm trying to preg_match the following regex, but it's giving me a "Delimiter must not be alphanumeric or backslash." I Googled the error, but the solutions were to add the slashes to the beginning and end of the statement. I already have those.

Code: Select all

/http:\/\/www\.youtube\.com\/watch\?v=(.*?)(\&(.*?))?/
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Delimiter must not be alphanumeric or backslash

Post by ridgerunner »

Your regex has valid syntax and does not generate any error when I place it directly into preg_match. However, it does fail to match the video value (group 1) because both dot-stars are lazy and can match nothing, and thus the regex successfully finishes before actually capturing anything. Changing each dot to a "not ampersand" and removing the lazy ? from the stars fixes the problem like so:

Code: Select all

<?php
$data = "http://www.youtube.com/watch?v=dkjdfij&v1=one&v2=two";
$re = '/http:\/\/www\.youtube\.com\/watch\?v=([^&]*)((?:\&[^&]*+)*)/';
if (preg_match($re, $data, $matches)) {
    print_r($matches);
}
else echo "No match\n";
?>
:)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Delimiter must not be alphanumeric or backslash

Post by pickle »

I'd just make my life simpler and use something else for the delimiter, such as ":".
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Delimiter must not be alphanumeric or backslash

Post by Cirdan »

Ah...the problem wasn't in my regex, it was because in an array I was passing to a third party library wasn't formatted right :P Thanks tho
Post Reply