user3755799
user3755799

Reputation:

Regex to get parts of URL

Hi I have URL as follows:

I need to parse the above URL to get two group as folloes:

Group1                 Group 2
vimeo.com/             99612902
www.vimeo.com/         99612902
http://vimeo.com/      99612902
http://www.vimeo.com/  99612902
http://vimeo.com/      81368903

I've tried the followin regex

^((http[s]?|ftp):\/)?\/?([^:\/\s]+)(:([^\/]*))?((\/[\w\-]+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?

but which yields me unwanted and empty group. Please help me out.

Upvotes: 1

Views: 224

Answers (3)

zx81
zx81

Reputation: 41838

With your input, we can match both parts into Groups 1 and 2 with this:

^(.*/)(.*)

or, for your revised input:

^(.*[/=])([^/=]+$)

In the demo, see the capture groups in the right pane.

In VB.NET, you can do this:

Dim theUrl As String
Dim theNumbers As String
Try
    ResultString = Regex.Match(SubjectString, "^(.*/)(.*)", RegexOptions.Multiline)
    theUrl = ResultString.Groups(1).Value
    theNumbers = ResultString.Groups(2).Value
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

Option 2

If you want to do some very lightweight url validation at the same time, you can use this:

^((?:http://)?(?:www\.)?[^./]+\.\w+/)(.*)

or, with your revised input:

^((?:http://)?(?:www\.)?[^./]+\.\w+[=/])([^/=]+$)

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Simply you could use the below regex,

^(.*\/)(.*)$

DEMO

From the starting upto the last / symbol are captured by group1. Remaining characters are captured into group2.

OR

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

DEMO

Upvotes: 0

Braj
Braj

Reputation: 46841

If you don't want to validate the url then try this as well. Get the matched group from index 1 and 2.

(.*?[^\/]*\/)(\d+)

Here is DEMO

String literals for use in programs: C#

@"(.*?[^\/]*\/)(\d+)"

Upvotes: 0

Related Questions