Regex to extract portion of URL

1.5k views Asked by At

The URL could be one of the following:

http://spappsdev.domain.com/sites/sitename/
http://spappsdev/sites/sitename/

I'd like to extract "spappsdev" in a way that would work for both URL's. Is this even possible? I'd hate to have to run two regex commands.

2

There are 2 answers

5
Gilles Quénot On BEST ANSWER

Try doing this using look around :

^https?://\K[^/\.]+

Or :

(?<=://)[^/\.]+

One real life example using in a shell :

$ perl -lne 'm!(?<=://)[^/\.]+! and print $&' file
spappsdev
spappsdev

To assign the result to a shell variable :

$ var="$(perl -lne 'm!(?<=://)[^/\.]+! and print $&' file)"
$ echo "$var"
5
anubhava On

You can use:

\bhttps?:\/\/([^\/.]+)

And grab matched group #1

RegEx Demo