Asked  7 Months ago    Answers:  5   Viewed   32 times

I have this regex for getting the YouTube video ID:

(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&n]+|(?<=v=)[^&n]+

I get it from there: Regex to parse youtube yid

The problem is I get preg_match() Unknown modifier '[' warning.

I know I have to enclose the regex delimiters but I have no idea how to do this.

Any help?

 Answers

25

Try the following:

<?php
  $str = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
  $pattern = '#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&n]+|(?<=v=)[^&n]+#';
  preg_match($pattern, $str, $matches); 
  print_r($matches);
?>

Note, I'm using # as a delimiter here simply because the regular expression above contains forward slashes and escaping them makes the expression more difficult to read. This cleans it up by just a few pixels.

Wednesday, March 31, 2021
 
khaverim
answered 7 Months ago
15

This will work only for non-nested parentheses:

    $regex = <<<HERE
    /  "  ( (?:[^"\\]++|\\.)*+ ) "
     | '  ( (?:[^'\\]++|\\.)*+ ) '
     | ( ( [^)]*                  ) )
     | [s,]+
    /x
    HERE;

    $tags = preg_split($regex, $str, -1,
                         PREG_SPLIT_NO_EMPTY
                       | PREG_SPLIT_DELIM_CAPTURE);

The ++ and *+ will consume as much as they can and give nothing back for backtracking. This technique is described in perlre(1) as the most efficient way to do this kind of matching.

Wednesday, March 31, 2021
 
KingCrunch
answered 7 Months ago
47

Your string contains a whole mess of / which would need to be escaped as / when using / as the regex delimiter. Instead of / as the regex delimiters, use something which won't occur in your string like ~ for example. You must choose a delimiting character which is guaranteed not to appear in $Src, however. You might be safer even with | than with ~.

$Src = 'images/pages/clients/logos/clnt_aljareera_img.jpg';
// Delimit the regular expression with ~
$pttn= '~&Src:'.$Src.'~';
$string=preg_replace($pttn,'',$string,1);

What has happened is your regex delimited by / encounters a p immediately after images/ because it thinks it has reached the closing delimiter. The next word pages is mistakenly treated as a string of regex modifiers.

PHP sees the regular expression:

/&src:images/pages
Wednesday, March 31, 2021
 
KingCrunch
answered 7 Months ago
23

You're missing a delimiter around your regular expressions:

if((preg_match('/[a-zA-Z]{1,7}+/',$_POST['naam'])) and (preg_match('/[^@#<>&*/]+[a-zA-Z0-9]+!/',$_POST['password'])))
Wednesday, March 31, 2021
 
nhunston
answered 7 Months ago
52

The standard disclaimer applies: Parsing HTML with regular expressions is not ideal. Success depends on the well-formedness of the input on a character-by-character level. If you cannot guarantee this, the regex will fail to do the Right Thing at some point.

Having said that:

<ab[^>]*>(.*?)</a>   // match group one will contain the link text
Saturday, May 29, 2021
 
lewiguez
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :