skip to Main Content

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


  1. To check if the line starts and ends with ‘*’ try regex pattern

    > ^*.**$
    
    Login or Signup to reply.
  2. This approach might suit your needs:

    import re
    teststring="*hello*?Q@*()@(UE) World?@(EI)@EN *"
    results=re.findall("^[*].*[*]$",teststring)
    if not len(results)==1:
        raise Exception
    

    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:

    re.findall("^[']{3}.*[']{3}$",teststring, re.DOTALL)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search