Search code examples
regexvariablesidentifier

RegEx matching variable names but not string values


It is hard to find. I need to write lexer and tokenizer for it. I've got a problem in finding a regex which matches variable names but not string values.

The following should not be matched:

"ala ma kota"
5aalaas

This should be matched:

ala_ma_KOTA999653
l90
a

I already got something like this:

[a-zA-z]\w+

but I don't know how to exclude " chars from the beginning and end of a match.

Thanks for any reply or google links (I couldn't find it - it can be from lmgify ;)).


Solution

  • I interpret variable names as all word character sequences with a min length of 1 and starting with a letter. Your regexp was almost correct then:

    ^[A-Za-z]\w*$