Viewed   89 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

3

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.

Saturday, September 3, 2022
4

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
Tuesday, August 2, 2022
 
5

The Arabic regex is:

[u0600-u06FF]

Actually, ?-? is a subset of this Arabic range, so I think you can remove them from the pattern.

So, in JS it will be

/^[a-z0-9+,()/'su0600-u06FF-]+$/i

See regex demo

Tuesday, October 11, 2022
5

You are using forward-slashes as your regex pattern delimeter, so /<title>(.*)</title>/Ui' will not work (</title> has a forward slash).

You should be able to escape the forward slash or use a different delimiter that is not contained within the pattern, for example

'/<title>(.*)</title>/Ui' //(esacaping)

or

'~<title>(.*)</title>~Ui' //different delimiter
Wednesday, August 17, 2022
3

For this PHP regex:

$str = preg_replace ( '{(.)1+}', '$1', $str );
$str = preg_replace ( '{[ '-_()]}', '', $str )

In Java:

str = str.replaceAll("(.)\1+", "$1");
str = str.replaceAll("[ '-_\(\)]", "");

I suggest you to provide your input and expected output then you will get better answers on how it can be done in PHP and/or Java.

Sunday, October 9, 2022
 
haodong
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :