MiseroMCS MiseroMCS - 1 year ago 97
Python Question

How can I find multiple of the same format in Python?

For a little idea of what the project is, I'm trying to make a markup language that compiles to HTML/CSS. I plan on formatting links like this: @(link mask)[(link url)], and I want to find all occurrences of this and get both the link mask and the link url.

I tried using this code for it:

re.search("@(.*)\[(.*)\]", string)

But it started at the beginning of the first instance, and ended at the end of the last instance of a link. Any ideas how I can have it find all of them, in a list or something?

Answer Source

The dfault behavior of a regular expression is "greedy matching". This means each .* will match as many characters as it can.

You want them to instead match the minimal possible number of characters. To do that, change each .* into a .*?. The final question mark will make the pattern match the minimal number of characters. Because you anchor your pattern to a ] character, it will still match/consume the whole link correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download