Maybe somebody can help me with this regex ?
.*\:\/\/(?:www.)?([^\/]+)(\/.+")
I need to get all paths from URL. I tried, but i can't match only path without quotation mark
Maybe somebody can help me with this regex ?
.*\:\/\/(?:www.)?([^\/]+)(\/.+")
I need to get all paths from URL. I tried, but i can't match only path without quotation mark
You can get the path using JSR223 Sampler with Groovy code.
Parse that URL to get protocol, host, port and path. Use JSR223 Sampler and paste the following code in Script area
URL url1 = new URL(vars.get('url'));
vars.put('protocol', url1.getProtocol());
vars.put('host', url1.getHost());
vars.put('port', url1.getPort() as String);
vars.put('path', url1.getPath());
vars.put('query', url1.getQuery());
Use that variables anywhere in the script using ${}
If you have to first scan for a URL:
I've attempted to provide a simple regex (overly simplified) that might work in your context, but you might have to modify it to provide some additional context. For example, x
is a valid path and this regex will recognize it as such. But if you are trying to look for the path in a string such as <img src="x">
, it will also recognize img
as a valid url path. In that case, you would want perhaps:
/<img\s+src="((https?|ftp):\/\/[^\/]+)?(\/?[^?#\s"]*)/i
var regex = /\b((https?|ftp):\/\/[^\/]+)?(\/?[^?#\s]*)\b/i;
var s = 'http://example.com/a/b?x=1';
var result = regex.exec(s);
console.log(result[3]);
If the protocol and host potion of the URL are always present, then it becomes easier to distinguish URLs in just about any context by making the protocol and host not optional:
/\b((https?|ftp)://[^/]+)(/?[^?#\s]*)\b/i;
You could go for something like:
(?:([^:\\/?#]+):)?(?:\\/\\/([^\\/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?
Demo:
More information: