Search code examples
regexjmeterpcre

How i can get PATH from URL with Regex?


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

https://regex101.com/r/J6nILD/6


Solution

  • You can get the path using JSR223 Sampler with Groovy code.

    1. Declare/ get the URL variable

    enter image description here

    1. 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());
      
    2. Use that variables anywhere in the script using ${}

    enter image description here enter image description here