I’ve been developing a Telegram bot with CMS with Markdown support. The tags supported are
*bold text*
_italic text_
`inline fixed-width code`
```text
pre-formatted fixed-width code block
```
the problem is when there is, say, an opening *
tag and no closing *
tag, the bot API breaks and refuses to send the message.
I’m not that much into regex, but is there a way to build a regex that validates all said tags when the message is constructed? or is there a ready-made markdown validator?
I tried python markdown
lib, meaning to catch an exception from it, but it doesnt break when the tags are broken, it just leaves the incorrect tags untouched.
2
Answers
To check if the line starts and ends with ‘*’ try regex pattern
This approach might suit your needs:
It assumes that a line starts with * and ends with *.
The _ and ‘ cases would follow a similar approach.
Because the ”’ case includes newlines, use the DOTALL flag: