0

I am trying to write a regex for a valid java identifier name: I have tried this:

([a-zA-Z_$][a-zA-Z\d_$]*) 

but it is accepting spaces also. However, I didn't allowed space to be considered as a match.

Any idea?

Alfabravo
  • 7,493
  • 6
  • 46
  • 82

1 Answers1

3

You need to include the start and end markers to enforce that the entire string matches the regex. Otherwise, it passes if any part of the string matches.

^([a-zA-Z_$][a-zA-Z\d_$]*)$

000
  • 26,951
  • 10
  • 71
  • 101