0
[a-zA-Z]_*[a-zA-Z0-9]*  

which I'm aiming for to match :

astring_something;
helloall90

but not :

Astring
_helloall

My regex is protecting me with the identifiers should start with small case letters. But it doesn't work for _ cases. Passing the string :

astring_something;

Is not been properly identified. Its been identified as astring and something leaving out _.

Where I'm making the mistake?

sriram
  • 8,562
  • 19
  • 63
  • 82

3 Answers3

2

I imagine you want it to start with a letter followed by zero or more letters, numbers or underscores. If so, you need to move the underscore into the second set of characters.

Change:

[a-zA-Z]_*[a-zA-Z0-9]*

To:

[a-zA-Z][a-zA-Z0-9_]*

Or, if it must start with a lowercase letter:

[a-z][a-zA-Z0-9_]*
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
1

You're currently matching only a single character before an underscore.

[a-zA-Z]*_[a-zA-Z0-9]*  

Whether or not that's what you really want is a different issue; what about things with multiple underscores, for example?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

Try

^[a-z]+_*[a-zA-Z0-9]+

Where ...

^[a-z]+

means it must start with one or more lower case letters, followed by ...

_*

zero or more _ characters, followed by ...

[a-zA-Z0-9]*

zero or more alphanumeric characters.

This pattern will match astring_something and helloall90 but will not match _helloall and Astring

Hitesh
  • 296
  • 2
  • 8