1

Hi i will have URL in following format:

It all must capture and return a domain name as youtube.

I have tried using

(http://|https://)?(www.)(.?*)(.com|.org|.info|.org|.net|.mobi)

but it showing error as regex parsing nested quantifier.

Please help me out

  • possible duplicate of [how to get domain name from URL](http://stackoverflow.com/questions/569137/how-to-get-domain-name-from-url) – Divi Jul 14 '14 at 06:43
  • @Divi Please consider before reporting as Duplicate. There is a difference and also i've tried those answer. nothing worked –  Jul 14 '14 at 06:44
  • Sorry but I fail to see the difference. You might want to edit your question – Divi Jul 14 '14 at 06:47

3 Answers3

2

If you are using a field that you know is in one of these formats, you can retrieve the match from Group 1 using this regex:

^(?:https?://)?(?:www\.)?([^.]+)

In VB.NET:

Dim ResultString As String
Try
    ResultString = Regex.Match(SubjectString, "^(?:https?://)?(?:www\.)?([^.]+)", RegexOptions.Multiline).Groups(1).Value
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try
zx81
  • 41,100
  • 9
  • 89
  • 105
  • Thanks for all you answer about regex. –  Jul 14 '14 at 06:56
  • I'm here right now, will wait a couple minutes then start a movie if I don't see you and return in a couple hours. :) – zx81 Jul 14 '14 at 10:03
  • Hi are you there. Can you please help me out for this http://stackoverflow.com/questions/24778609/parse-url-using-regex –  Jul 16 '14 at 10:38
1

(.?*) should be (.*?) - that's the source of your error.

Also, remember to escape the dot unless you want it to match any character.

And since the www. part is optional, you need to add a ? quantifier to that group as well.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

You could try the below regex to get the domain name youtube from the above mentioned URL's,

^(?:https?:\/\/)?(?:www\.)?([^.]*)(?=(?:\.com|\.org|\.info|\.net|\.mobi)).*$

DEMO

It ensures that the domain name must be followed by .com or .info or .org or .net or .mobi.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274