Match URL Scheme with Port - regex

I'm trying to match the the URI / URL scheme (e.x. ftp, http, https) using this regex ^(.*:) . The problem is that my url has a port number so the : to connote server connection in the scheme isn't the only one in the URL there is also :80. How do I just match the scheme using regex?
Given this example:
http://video.google.co.uk/videoplay?docid=-7246927612831078230&hl=en#00h02m30s
I would like regex to just match:
http
I'm not interested in something like
^((http[s]?|ftp):\/)
I know this would work it, it is limiting however. It wouldn't give me mailto, tel, ssh, etc.

I just found that ^[^:]+ works.

Related

Need regular expression for Spring controller GetMapping with path variable that excludes URLs with a certain prefix

I am trying to create a regular expression for my Spring controller method that matches all of the paths in my web application, except It also needs to exclude any traffic with the prefix “/websock” that comes in for my websocket server. Here is the method:
#GetMapping(value = "/{path:[^\\.]*}")
public String redirect(#PathVariable(value="path") String path) {
LOGGER.debug("In redirect path:" + path);
return "forward:/";
}
I have tried a lot of examples from the web I have not been able to find one that does what I need. My examples have either accepted all traffic (including the websocket prefix), or that have excluded some of the http traffic.
The first reg-ex was
"/{path:[^\\.]*}"
which matches URLs such “http://localhost:8080/abc”. But, it was failing to match http://localhost:8080/abc/def.
My second attempt using
"/**/{path:[^\\.]*}"
corrected that issue but then matched everything including my websocket traffic.
I tried the following, which I saw from other questions, was supposed to exclude traffic starting with "/websock". But instead it failed to match anything including the “abc” URLs above:
"/{path:[^\\.]*}", "/**/{path:^(?!websock).*}/{path:[^\\.]*}".
What am I doing wrong with my reg-ex?

Regex to find URL including those that simply start with // (Protocol-relative URLs)

This Regex finds URLs that begin with http and https
https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)
I am trying to figure out how to modify this to including those URLs which omit the http or https part. I understand that these are called 'Protocol-relative URLs'
example: //example.com and not http://example.com
Simply make the protocol part optional:
(https?:)?(\/\/)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)
By the way I assume you really wanted example.com and not //example.com (since no one writes a URL that way).

Regular expression for Google analytics for matching the domain name in a URL

I am too new to regular expressions and my effort at it so far seems futile. I'm trying to write a very simple regexp to match a domain name like google.com with any URL referrer like https://www.google.com.
So far I have tried /*google.com*/, (*google.com*) and [*google.com*] and none of these have worked.
Any help would be greatly appreciated.
Try the below regular expression. I assume you are applying this on the Hostname dimension.
.*google\.com.*
I wrote you a regex which matches prefixes, such as e.g.: https, http, ftp & sftp.
The regex can be found here:
http://rubular.com/r/7sLhefh2xE

Regex for sub-domain

I have several sub-domains configured to a IIS website. I would like to parse the incoming requests in ARR. I would like to match a specific sub-domain that would capture all these different scenarios
http://abc.example.com
https://abc.example.com
http://abc.example.com/xyz
https://abc.example.com/xyz
http://abc.example.com/xyz?q=123
https://abc.example.com/xyz?q=123
I have tried a few things but they don't seem to work and searches only reveal how to catch sub-domains and not just a sub-domain.
Thanks
Try this:
http(?:s)?:\/\/(abc)\..*

How to write regex for apache ProxyPassMatch to reverse proxy API calls

I have an angular 4 web application which is hosted on apache 2.4. The application makes use of an API written in nodejs javascript running over express. Both the website and the API service are running on the same machine but on different ports. The website is on port 80 and the API service is listening on port 9000.
I would like to set up apache to do reverse proxy for all the API calls.
For example, any url that contains /api/ I want it rewritten by apache to point to the API url:port. If I use ProxyPass like the following lines, the redirect works fine:
ProxyPass "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
ProxyPassReverse "/api/V1/systeminfo" "http://localhost:9000/api/V1/systeminfo"
What I do not know how to do, is to use the ProxyPassMatch directive and create a regular expression so that any url that contains /api/ is redirected to http://localhost:9000/api/.....
I tried the following but it does not work:
ProxyPassMatch "^/api.*$" "http://localhost:9000/$1"
ProxyPassReverse "^/api.*$" "http://localhost:9000/$1"
Neither does the following:
ProxyPassMatch "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
ProxyPassReverse "^/.*?/api.*?/v[0-9]+/(.*)$" "http://localhost:9000/$1"
Any help would be appreciated. My regex skills are lacking!
Note: obviously 'localhost' can be an IP address or a domain, I am using it in the example for simplicity.
Many thanks!
Edit: I corrected the first example to use .* instead of just * as per Alex's comment.
I solved the problem. The correct way to do reverse proxy with apache on the above example is the following:
ProxyPassMatch "/api(.*)" "http://localhost:9000/api$1"
ProxyPassReverse "/api(.*)" "http://localhost:9000/api$1"
I knew the multiple regex examples I was trying were correct, as I was testing them with https://regex101.com/, but I was hard coding the second part of to a particular route in order to eliminate the issue of the second part being incorrect, but for some reason it does not like that. Once I understood that the (.*) part of the regex is the first capture group and used it as $1 in the second part, it all worked.
I hope I clarified the answer enough and it is useful to someone else.