Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex to allow only alpha numeric

Tags:

c#

regex

I have the following regex ^[a-zA-Z0-9]+$ which would allow alpha numeric characters. The problem here is that if I enter only numeric character like "897687", then the regex still matches. I don't want that to happen. There should be at least one text character and it should start with a text character. For example like "a343" or "a98bder" or "a4544fgf343"

It would be great if you could help me to improve my regex for this.

like image 787
Derin Avatar asked Nov 15 '11 08:11

Derin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Sounds like you want:

^[a-zA-Z][a-zA-Z0-9]*$ 

EXPLANATION

^ asserts position at the start of a line

Match a single character present in the list below [a-zA-Z]

» a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)

» A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)

Match a single character present in the list below [a-zA-Z0-9]*

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)

A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)

0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

$ asserts position at the end of a line

Demo

like image 92
Rowland Shaw Avatar answered Oct 06 '22 06:10

Rowland Shaw