0

I am trying to get all the matches with a string starting with [& and ends with ] (for example [&Prueba]).

So far I have this expression \[&.*] The problem is that if I have two or more matches, it considered them as one, I mean for example

dsdd [&xD]s wpxxxxx.php [&dsdd]

The regular expression throws one match, this: [&xD]s wpxxxxx.php [&dsdd] and I want to get two results: [&xD] and [&dsdd]

How can I get that? I have tried several things and I'm still not getting it.

Thanks and regards!

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • 1
    Try `\[&[^\]]*]` or `\[&.*?]` – ctwheels Mar 15 '18 at 17:28
  • Possible duplicate of [My regex is matching too much. How do I make it stop?](https://stackoverflow.com/questions/22444/my-regex-is-matching-too-much-how-do-i-make-it-stop) – ctwheels Mar 15 '18 at 17:29

1 Answers1

1

You'll want to do something like this:

\[&[^\]]+\]

Here is an example in python:

import re

data = "dsdd [&xD]s wpxxxxx.php [&dsdd]"
results = re.findall("\[&[^\]]+\]", data)
print (results)

['[&xD]', '[&dsdd]']

The key part is the negative character class surround by the square brackets (with the first one having an ampersand). That matches [ , all non ] and finally ]

This is a good site to play around with stuff like this: https://regex101.com/

sniperd
  • 5,124
  • 6
  • 28
  • 44