pythonpyramidcolander

How to write a regular expression in Python that accepts alphabets, numbers and a few selected special characters(,.-|;!_?)?


A Regular Expression in Python that accepts letters,numbers and only these special characters (,.-|;!_?).

I have tried solving the problem through the following regular expressions but it didn't work:

'([a-zA-Z0-9,.-|;!_?])$'
'([a-zA-Z0-9][.-|;!_?])$'

Can someone please help me write the regular expression.


Solution

  • I think the following should work (tested on RegExr against Foo123,.-|;!_?):

    ^[\w,.\-|;!_?]*$
    

    In your regular expressions, you forget to escape the '-' character, which is interpreted as a range of characters to match against.