This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Regexp match

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Regexp match

Post by caesar »

Hi,

would appreciate if someone with far better knowledge of regexp could help me out turning this regexp from PHP into TCL:

Code: Select all

preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
Thanks.

PS: Taken from here cos it matches multiple patterns.
Once the game is over, the king and the pawn go back in the same box.
A
AlbozZz
Voice
Posts: 3
Joined: Sun Jan 26, 2020 5:59 pm

Hi

Post by AlbozZz »

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 ...)}
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Great. Thanks!
Once the game is over, the king and the pawn go back in the same box.
Post Reply