pythonpython-2.7jythonjython-2.7

Check that the "alpha" part of a string consists of only a certain sequence of characters - python


I am developing a listener to check if in a string representing a formula there are only vg digraphs such as "alpha" characters (a, b, c, etc ..).

Eg:
vg/1000 * 2 Correct!
vl/1000 * 2 Wrong!

For now I have tried with regular expressions, using the following:
(.*vg.*)+ but I have the impression that it is not the best solution.


I'm thinking about the best way to write this check in python.
Any tips?
Thanks in advance!


Solution

  • If you want a regular expression that only accepts vg as a sequence of letters, then:

    '^(\d*([\.\,]\d+)?[-+/*]?(vg)?)*$'
    

    Otherwise if you want to choose from a set of letter sequences:

    '^(\d*([\.\,]\d+)?[-+/*]?(vg|otherSequence)?)*$'