1

I am trying to use python regex back reference to match all instances for the first group match, but it is stopping at the first match. How can i match all the occurrences? For example:

var req = new XMLHttpRequest()
req.open('POST', 'http://example.com', false);
req.send(null)

My first group match is req, and then if req is found, I am trying to match req.open and req.send, but I am only getting a match on req.open.

The pattern I am using is

([a-zA-Z_$][0-9a-zA-Z_$]*)\s?=\s?new XMLHttpRequest.+?(\1\.((open|send)\(.+?\)))

Here is the regex101

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
securisec
  • 3,435
  • 6
  • 36
  • 63
  • `([a-zA-Z_$][0-9a-zA-Z_$]*)\s?=\s?new XMLHttpRequest(?:.+?(\1\.((open|send)\(.+?\))))+`? See https://regex101.com/r/VlWVgy/1 – Wiktor Stribiżew Jun 27 '19 at 13:34
  • @WiktorStribiżew in your regex101, it is only matching `req.send`. I would like to match both `req.send` and `req.open` – securisec Jun 27 '19 at 13:37
  • So you want to access all captures of the repeated capturing groups. The regex shared works as you need, see [this answer](https://stackoverflow.com/a/9765390/3832970) how. Use it with PyPi regex module and access the `.captures` of the necessary group. – Wiktor Stribiżew Jun 27 '19 at 13:42
  • @WiktorStribiżew this is not a duplicate! The issue mentioned is not the same. one is for findall, but in this case, findall doesnt solve the need – securisec Jun 27 '19 at 13:42
  • `findall` won't help. You need to wrap the repeated part with a non-capturing group - see my pattern - then access that group `.captures` after running the regex with `regex.finditer`. See [**this answer**](https://stackoverflow.com/a/9765390/3832970) – Wiktor Stribiżew Jun 27 '19 at 13:49
  • @WiktorStribiżew please reopen the post. I have tried that link that you posted, and just like in regex101 examples, it is only matching on the `req.send` and not both req.send and req.open. The issue are your relating to this SO post is not the same. When I run the python code from the reference post, I am only getting `('req', 'req.send(null)', 'send(null)', 'send')` which does not solve the original question – securisec Jun 27 '19 at 15:26
  • That solution [works very well](https://rextester.com/VYEY25873), why reopen? – Wiktor Stribiżew Jun 27 '19 at 15:32
  • Ah. i failed to notice the use of the `regex` lib vs stdlib `re`. Thanks – securisec Jun 27 '19 at 15:44

0 Answers0