-1

There is a line of text:

Lorem ~Ipsum~ is simply ~dummy~ text ~of~ the printing...

To find all the words enclosed in ~~ I use

re.search(r'~([^~]*)~', text)

Let's say it became necessary to use ~~ instead of ~ ([^\~]+) indicates to exclude the ~ character from the text within those characters How do I make a regular expression to exclude a string of characters instead of just one?

That is, ~~Lor~em~~ should return Lor~em

The symbol of the new string must not be excluded and the length of the found string cannot be 0

Aem Aem
  • 95
  • 7

1 Answers1

1

Use a non-greedy quantifier instead of a negated character set.

re.search(r'~~(.*?)~~', text, flags=re.DOTALL)

re.DOTALL makes . match newline characters.

Barmar
  • 741,623
  • 53
  • 500
  • 612