Search code examples
regexnintex-workflow

Regex to extract portion of URL


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.


Solution

  • 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"