python - Regex not beginning with number -


How do I create a regex that matches all alphanumerics without any number at the beginning?

For example, 1B will not be a match, AB1 will match, 1_BC will not match, I have "^ [0-9] [a-zA-Z0- 9_] Firstly, to deny a class square, you have inserted the code ^ inside Not in front of them ^ [0-9] means "any digit, at the beginning of the string"; [^ 0-9] means "anything except a digit".

Second, [^ 0-9] matches anything this is not a digit, not just letters and underscores. You really want to say that the first letter is "no number, but there is a number, letter, or underscore", right? Although it is not impossible , it is very easy to merge into saying "is a letter or underscore."

In addition, you forgot to repeat the last character set. As you are matching exactly two letters, therefore b1 will work, but b12 will not be.

like this:

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

< Img src = "https://www.debuggex.com/i/5Eh1fYCkVJt3iG1Z.png" alt = "Regular Expression Visualization">

In other words: a letter Or after underscore, zero or more letters, digits or underscores.

I'm not completely sure that this is what you really want, at least if regex is your complete parser, for example, in foo-bar Do you want to bar match? If so, in the 123spam , do you want spam to match? But this is what you were trying to write.


Comments