Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx Start With Letter or Number 0

Tags:

regex

I'm trying to write a RegEx that starts with the number 0 or ANY Letter (basically it can't start with 1-9). It also must be 13 characters long

I have this but it does not seem to work:

"^[0][a-zA-Z]{13}"
like image 617
mint Avatar asked Mar 22 '12 00:03

mint


People also ask

How do you match a number or letter in regex?

To match all numbers and letters in JavaScript, we use \w which is equivalent to RegEx \[A-za-z0–9_]\ . To skip all numbers and letters we use \W .

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Which character starts with in regex?

As usual, the regex engine starts at the first character: 7. The first token in the regular expression is ^. Since this token is a zero-length token, the engine does not try to match it with the character, but rather with the position before the character that the regex engine has reached so far.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


2 Answers

Try this regex instead:

"^[0a-zA-Z].{12}$"

[0a-zA-Z] means "one character, that is either a 0, one of a-z, or one of A-Z".

.{12} means twelve more characters, whatever they are.

like image 156
Amber Avatar answered Sep 20 '22 02:09

Amber


You are looking for something like this:

^[0\w]\S{12}$

This states the first character must be 0 or a word character. Followed by exactly 12 characters that are NOT whitespace (this allows 0-9, characters and special characters. Repace the \S with anything you like. Such as [0-9\w]

A great playground to test regular expressions is at: http://regexpal.com/ I use it constantly to test regular expressions.

like image 26
Taylor Dondich Avatar answered Sep 21 '22 02:09

Taylor Dondich