0

I need a regex to use with Chrome's network log to show only API calls except to two specific endpoints:

Ex.:

I want to show everything containing /api/:

http://localhost:8081/api/software-features
http://localhost:8081/api/user-groups/logged-user-group
http://localhost:8081/api/user-groups/%7B4D7E2585-32E3-4794-A0F3-4FE7D56D27AE%7D/permissions/system
http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras
etc...

Except when it ends with /image-640x480.jpg or /statistics:

http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras/0/streams/0/statistics
http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras/0/streams/0/image-640x480.jpg

These two are constantly requested by the front-end and cause noise in the log.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Bruno Finger
  • 2,105
  • 3
  • 27
  • 47
  • 1
    You've told us what the regular expression is failing to do, but haven't shown us the regular expression. – byxor Nov 22 '16 at 12:53
  • https://regex101.com/ try and learn by yourself – arminb Nov 22 '16 at 12:54
  • I tried at http://www.regexr.com/ but everything is failing. It's a very complex case for my regex-foo. I wouldn't be asking for help if I wouldn't be needing it. I mean they say that you can "get help from experts" in this place, right? – Bruno Finger Nov 22 '16 at 12:57
  • What regex flavor? – Nicolas Nov 22 '16 at 12:58
  • @NicolasMaltais It's to use on Chrome's developer console, in the filter on the Network tab. I'm not sure what are the limitations there. – Bruno Finger Nov 22 '16 at 13:00
  • Since you've mentioned learning regex. Try this [regexone.com](http://regexone.com) It has exercises and is better than most sites that teach regex. – Abhirath Mahipal Nov 22 '16 at 13:00
  • Well, if the string starts with `/api`, you may try `^/api(?!.*(?:[.]jpg|statistics)$)` – Wiktor Stribiżew Nov 22 '16 at 13:00
  • @WiktorStribiżew It works, thanks! Do you mind writing an answer explaining it? – Bruno Finger Nov 22 '16 at 13:01
  • Possible duplicate of [Unix grep regex containing 'x' but not containing 'y'](http://stackoverflow.com/questions/6063258/unix-grep-regex-containing-x-but-not-containing-y) – Maximilian Peters Nov 22 '16 at 13:32

3 Answers3

1

To match a string starting with /api and not ending with a couple of alternatives, you need to use a regex with a negative lookaround:

^/api(?!.*(?:[.]jpg|statistics)$)

Details:

  • ^ - start of a string
  • /api - a literal text at the start of the string
  • (?!.*(?:[.]jpg|statistics)$) - a negative lookahead that fails the match if there are any 0+ chars other than linebreak symbols after /api and there are .jpg ([.]jpg) or (|) statistics at the end of the string ($).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Not a regular expression, but i believe it solves the problem:

(str.search(/\/api/i) >=0 && str.search(/\.jpg/i) == -1 && str.search(/\/statistics/i) == -1 )

Trying this on your samples:

const urls = [
  "http://localhost:8081/api/software-features",
  "http://localhost:8081/api/user-groups/logged-user-group",
  "http://localhost:8081/api/user-groups/%7B4D7E2585-32E3-4794-A0F3-4FE7D56D27AE%7D/permissions/system",
  "http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras",
  "http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras/0/streams/0/statistics",
  "http://localhost:8081/api/servers/%7B499C3279-CD5D-402E-A60B-6AE0B5A21EE0%7D/cameras/0/streams/0/image-640x480.jpg"
];

console.log([for (url of urls) (url.search(/\/api/i) >=0 && url.search(/\.jpg/i) == -1 && url.search(/\/statistics/i) == -1 )])

// Array [ true, true, true, true, false, false ]
Alexander
  • 12,424
  • 5
  • 59
  • 76
1

You can try this regex too, it works in most regex engines

^.*\/api\/(?!.*(?:\.jpg|statistics)$).*$
  • ^ matches start of the string
  • .*\/api\/ matches \api\ and everything before
  • (?!.*(?:\.jpg|statistics)$) this is a negative lookahead, the match will stop if next characters are anything .* followed by .jpg or statistics and the end of the string
  • .*$ matches everything after /api/ in case the negative lookahead doesn't match
Nicolas
  • 6,611
  • 3
  • 29
  • 73