-1

For example, I have strings like these, there could be any characters between two opening and closing curly braces. So, what could be the regex for these examples?

{{.registry}}
{{ if de .proxy ""  }}
{{ end -}}
{{.test_client}}

I was trying this, but it´s not working.

/{{([^}]*)}}/
Misha
  • 49
  • 7
  • How is the above pattern not working? [It works](https://regex101.com/r/WyDmBv/1). However, this question has been asked so many times, I added three sample duplicate links. – Wiktor Stribiżew Jul 21 '20 at 08:59
  • Python doesn't use slashes around regular expressions. If that's literally what you put in a string and passed to `re.findall()` or similar, you are looking for braces immediately inside a pair of literal slashes. – tripleee Jul 21 '20 at 09:23

1 Answers1

3

You may take the lazy star as in

{{(.*?)}}

See a demo on regex101.com. Honestly, for your given example strings, your expression works equally well, albeit without the slashes, see another demo on regex101.com.


So, in Python you could use

import re

inp = """{{.registry}}
{{ if de .proxy ""  }}
{{ end -}}
{{.test_client}}"""

rx = re.compile(r'{{(.*?)}}')
matches = rx.findall(inp)
print(matches)

Which yields (whitespaces intended):

['.registry', ' if de .proxy ""  ', ' end -', '.test_client']
Jan
  • 42,290
  • 8
  • 54
  • 79