1

I have tried previous answers here on SO. I was able to find only one subset of several.

here is the code and sample that I am working on.

s = "{| mySting0 |}  The {| mySting1 |}  The {| mySting2 |}  The {| mySting3 |}  make it work "

result = re.findall('{\|(.*)|}', s)

the output is,

[' mySting0 |}  The {| mySting1 |}  The {| mySting2 |}  The {| mySting3 |}  make it work ']

What am I doing wrong?

1
  • 5
    "What am I doing wrong?" - using a greedy regex, not escaping the second pipe character with a backslash. Commented Sep 30, 2016 at 19:51

1 Answer 1

7

You can use this regex:

>>> s = "{| mySting0 |}  The {| mySting1 |}  The {| mySting2 |}  The {| mySting3 |}  make it work "
>>> re.findall(r'{\|(.*?)\|}', s)
[' mySting0 ', ' mySting1 ', ' mySting2 ', ' mySting3 ']

Changes are:

  1. Use lazy quantifier .*? instead of greedy .*
  2. Excape 2nd | as well in your regex
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.