What's a regex for finding a url?

#16
by osanseviero - opened

Question:

What's a regex for finding a url?

Answer:

There are a few variations of regular expressions to match URLs. They all depend on the context.

  • Matching URLs in a text file

For that, I’d recommend RegexBuddy, which lets you test your expressions and can show you the parts that match and don’t match.

  • Matching URLs in a search engine

For that, there are many more specific variations of regular expressions. Here’s one of them:

(?:https?://)(?:(?:(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)%?)|(?:(?:[a-z](?:[a-z0-9]{2,}\.?)?)(?:\.?)?))(?:/(?:(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)%?)|(?:(?:[a-z](?:[a-z0-9]{2,}\.?)?)(?:\.?)?))(?:#(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)%?)|(?:(?:[a-z](?:[a-z0-9]{2,}\.?)?)(?:\.?)?))(?:/(?:[a-z](?:[a-z0-9-]{0,61}[a-z0-9])?)%?)|(?:(?:[a-z

Sign up or log in to comment