4

I'm trying to create a regular expression to use as a rule in fiddler. I'm not very good at regular expressions.

This regular expression:

http://myServer:28020/MyService/ItemWebService.json?([a-zA-Z]+)

Matches the URL below:

http://myServer:28020/MyService/ItemWebService.json?action=keywordSearch&username=StockOnHandPortlet&sessionId=2H7Rr9kCWPgIZfrxQiDHKp0&keywords=blue&itemStatus=A

So far so good. But why when I try this regular expression:

http://myServer:28020/MyService/ItemWebService.json?action=keywordSearch([a-zA-Z]+)

It does not match the above URL. Why would that be?

3
  • tried that. It doesn't work either. Does the & need to be escaped? Commented Jul 12, 2015 at 11:36
  • What do you need to match? The whole URL? Only the query string? Or the part before the query string? Mind that . and ? must be escaped outside of character class. Commented Jul 12, 2015 at 11:43
  • the whole url. Before fiddler intercepts the http requests it wants to check that a url matches the pattern specified. Commented Jul 12, 2015 at 11:47

2 Answers 2

11

In a regular expression, you need to escape . and ? outside of character class.

So, this is enough to match the URL itself:

http://myServer:28020/MyService/ItemWebService\.json

URL with query string can be matched with

http://myServer:28020/MyService/ItemWebService\.json\??(?:&?[^=&]*=[^=&]*)*

See demo

Explanation:

  • http://myServer:28020/MyService/ItemWebService\.json - matches the base URL where we need to escape . to match it literally
  • \?? - matches 0 or 1 (due to ? quantifier) literal ?
  • (?:&?[^=&]*=[^=&]*)* - matches 0 or more (due to *) sequences of &?[^=&]*=[^=&]*:
    • &? - 0 or 1 & (no need escaping)
    • [^=&]* - 0 or more characters other than = and &
    • = - n equals sign
    • [^=&]* - 0 or more characters other than = and &

If you want to match a URL that has the first query parameter=value set to action=keywordSearch, you can use the following regex:

http://myServer:28020/MyService/ItemWebService\.json\?action=keywordSearch(?:&?[^=&]*=[^=&]*)*
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. This worked for me. I understand your explanation as well about escaping. Can you elaborate on the bit inside the brackets for us beginners.
thanks. Actually sorry to bother but could you modify you answer to include the action=keywordSearch. I was always able to match the url without the action part but it was when i added the action part that i couldn't get the regex right.
Ok, I added that version.
2

Use

(\?|\&)([^=]+)\=([^&]+)

With g option (global)

http://myServer:28020/MyService/ItemWebService.json(\?|\&)([^=]+)\=([^&]+)

3 Comments

Sorry but fiddler also said that the url does not match the pattern
Add your URL in front of it
just using ([^=]+)\=([^&]+) matching asd and asd=asd and also asd=.... how do I use this with pattern attribute to match only the query string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.