Hi guys

this is my very first python program

I've been trying to use re.match to test to see if a string is an identifier
as defined by this syntax

identifier -> letter{ letter| digit}

I tried the following statements
but when i test my program every single time it gets to a number by its self it still thinks its an identifier which is incorrect..

what am i missing??
your assistance is crucial

Thank you

if re.match("^[\w\d_-]+$", i):
    if re.match("^[A-Za-z0-9_-]*$",i):
    if re.match('\w+(\w\d)+?', i):
5 Years
Discussion Span
Last Post by coconutdumplin

Try this

>>> import re
>>> ident_re = re.compile(r"^[a-zA-Z_]\w*$")
>>> ident_re.match("hello")
<_sre.SRE_Match object at 0x7f9b9eac24a8>
>>> ident_re.match("f8888")
<_sre.SRE_Match object at 0x7f9b9eac2510>
>>> ident_re.match("8888")

Always use strings prefixed with 'r' like in r"..." for regular expressions.

Edited by Gribouillis: n/a


thanks Gribouillis for responding...
can you tell me what your code means?....
this is my function...i'm unsure exactly how to fit your code in there

def Identifiers(seq,i):
import re
    if re.match(r"^[a-zA-Z_]\w*$",i):

             return True
            return False

where seq is a string that contains what ever is on each line of a file and i each element in that string

i see where you test to see if its a letter... but what if its something like 1d or dd3 or a3d4....which are all valid identifiers...how would it be able to recognize this..??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.