Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex not beginning with number

Tags:

python

regex

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

Right now I have "^[0-9][a-zA-Z0-9_]"

For example, 1ab would not match, ab1 would match, 1_bc would not match, bc_1 would match.

like image 612
Apollo Avatar asked Oct 27 '14 20:10

Apollo


People also ask

How to check if a string does not start with?

To check if a string does not start with specific characters using a regular expression, use the test() function and negate it. Make sure your regular expression starts with ^ , which is a special character that represents the start of the string.

What is ?! In regex?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.


1 Answers

There are three things wrong with what you've written.

First, to negate a character class, you put the ^ inside the brackets, not before them. ^[0-9] means "any digit, at the start of the string"; [^0-9] means "anything except a digit".

Second, [^0-9] will match anything that isn't a digit, not just letters and underscores. You really want to say that the first character "is not a digit, but is a digit, letter, or underscore", right? While it isn't impossible to say that, it's a lot easier to just merge that into "is a letter or underscore".

Also, you forgot to repeat the last character set. As-is, you're matching exactly two characters, so b1 will work, but b12 will not.

So:

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

Regular expression visualization

Debuggex Demo

In others words: one letter or underscore, followed by zero or more letters, digits, or underscores.

I'm not entirely sure this is what you actually want, at least if the regex is your whole parser. For example, in foo-bar, do you want the bar to get matched? If so, in 123spam, do you want the spam to get matched? But it's what you were trying to write.

like image 176
abarnert Avatar answered Oct 16 '22 18:10

abarnert