-2

I'm trying to find strings that are surrounded by asterisks and pipelines, for example: *|FOO|*, *|BAAAAAR|* I already researched several other answers, but every time I combine asterisks with pipelines, it does not work. Can anyone help me?

And I need to find all the possibilities in a same text, for example:

Hello *|FOO|*, let's play with *|BAAAAAR|*

I need to find both '*|FOO|*' and '*|BAAAAAR|*'.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
ThCC
  • 141
  • 1
  • 2
  • 12
  • Post the code you tried – Mr. E Nov 03 '15 at 13:09
  • I try using these codes: http://stackoverflow.com/questions/6646448/how-can-i-get-all-content-between-two-pipes-using-regular-expression http://stackoverflow.com/questions/17123502/regular-expression-match-substring-between-pipes http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace http://stackoverflow.com/questions/20753090/regex-to-match-string-between-asterisk-with-line-breaks Just to be clear: i suck at regex – ThCC Nov 03 '15 at 13:10

1 Answers1

3

Use re.findall. Since * and | are special characters in regex, you must escape that in-order to match literal *, | characters.

>>> s = "Hello *|FOO|*, let's play with *|BAAAAAR|*"
>>> re.findall(r'\*\|.*?\|\*', s)
['*|FOO|*', '*|BAAAAAR|*']
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • I just saw your correction Raj. It seems to be working. In 5 minutes I will accept your answer – ThCC Nov 03 '15 at 13:17