The Tcl and PHP RE languages are quite similar in this area (there are areas where they diverge, but this RE doesn't use them). The behaviour of using a match array in PHP is much like using the -inline option to regexp and saving the result (a list) in a variable that can then be indexed into (e.g., with lindex). The wrapping %…%i part of the PHP code becomes the -nocase option. Finally, you'll want to put REs in Tcl in {braces}.
Code: Select all
set match [regexp -inline -nocase {(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})} $url]
set youtube_id [lindex $match 1]
We can split that into more lines for readability.
Code: Select all
set RE {(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})}
set match [regexp -inline -nocase $RE $url]
set youtube_id [lindex $match 1]
The first line here is storing the regular expression in a variable, and the second line applies the RE to whatever is in the url variable, storing the resulting list in the match variable for later examination. Which is a good match for what preg_match was doing over in PHP.
Since you only actually want the ID, that's easy too:
Code: Select all
set RE {(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})}
regexp -nocase $RE $url --> youtube_id
If desired, you can embed the caselessness inside the RE, similar to the PHP case, by prepending
Code: Select all
(?i): set RE {(?i)(?:youtube ...)}