Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:29:37+00:00 2026-06-08T11:29:37+00:00

I have the following regex from here: https://stackoverflow.com/a/10405818/924999 val regex = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/ig;.r I’m attempting

  • 0

I have the following regex from here: https://stackoverflow.com/a/10405818/924999

val regex = """/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/ig;""".r

I’m attempting to extract the video ID from youtube video urls with:

val url = "http://www.youtube.com/watch?v=XrivBjlv6Mw"

url match {

    case regex(result) => result

    case _ => null

}

However it seems to always return null, is there something I’m missing or need to do differently?

Thanks in advance for any help, much appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:29:39+00:00Added an answer on June 8, 2026 at 11:29 am

    The regex that you have is php-style regex, not java-style – for example, note /ig; flags at the end.

    So you’ll just have to edit it a bit:

    val youtubeRgx = """https?://(?:[0-9a-zA-Z-]+\.)?(?:youtu\.be/|youtube\.com\S*[^\w\-\s])([\w \-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>|</a>))[?=&+%\w-]*""".r
    

    I tested it on all possible youtube urls, and it works. Example:

    scala> youtubeRgx.pattern.matcher("http://www.youtube.com/watch?v=XrivBjlv6Mw").matches
    res23: Boolean = true
    

    And extracting the value:

    "http://www.youtube.com/watch?v=XrivBjlv6Mw" match {
      case youtubeRgx(a) => Some(a) 
      case _ => None 
    }
    res33: Option[String] = Some(XrivBjlv6Mw)
    

    It’s a pity that java does not allow proper comments in regexps, so I did what I could:

    val youtubeRgx = """https?://         # Required scheme. Either http or https.
                       |(?:[0-9a-zA-Z-]+\.)? # Optional subdomain.
                       |(?:               # Group host alternatives.
                       |  youtu\.be/      # Either youtu.be,
                       || youtube\.com    # or youtube.com followed by
                       |  \S*             # Allow anything up to VIDEO_ID,
                       |  [^\w\-\s]       # but char before ID is non-ID char.
                       |)                 # End host alternatives.
                       |([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
                       |(?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
                       |(?!               # Assert URL is not pre-linked.
                       |  [?=&+%\w]*      # Allow URL (query) remainder.
                       |  (?:             # Group pre-linked alternatives.
                       |    [\'"][^<>]*>  # Either inside a start tag,
                       |  | </a>          # or inside <a> element text contents.
                       |  )               # End recognized pre-linked alts.
                       |)                 # End negative lookahead assertion.
                       |[?=&+%\w-]*       # Consume any URL (query) remainder.
                       |""".stripMargin.replaceAll("\\s*#.*\n", "").replace(" ","").r
    

    (adapted from @ridgerunner’s answer here: find all youtube video ids in string)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked here and from what I understand the following regex simply means
I have written the following regex to match a set of e-mails from HTML
Following up from my last question @ stackoverflow.com/questions/7049245/ I got a couple of answers
I have the following so far: ^((http[s]?|ftp):\/\/)(([^.:\/\s]*)[\.]([^:\/\s]+))(:([^\/]*))?(((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?)?$ Been testing against these: https://www.google.com.ar:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash https://google.com.ar:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash https://google.com:8080/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
So i'm looking to scrape rapidshare.com links from websites. I have the following regular
I have the following regex from regex lib that does a fine job to
I have following regex (abc|def)( ?(\\d+|(?:(?!\\1)[a-z])+)?)* with matches perfectly the subject abc123 456 .
I have the following regex date validator but it is allowing the following date:
I have the following regex which validates dates: //are not both dates if (!methods._isDate(first[0].value)
I might have pretty basic question about regex. I have the following regex, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.