I need a regular expression to check that a string contains combination of alphabets and numbers.Special characters and space is not allowed.my string should contain atleast one character and one alphabet
-
1Have you tried anything? I think regular expressions are pretty good covered with tutorials and examples, maybe you should use a search engine... – Matten May 01 '13 at 08:30
-
1Can you elaborate on your question? This is quite a bit vague... http://gskinner.com/RegExr/ might be a start for you ;) – Ken de Jong May 01 '13 at 08:31
-
Possible Duplicate of [Regular Expression for alphanumeric and underscores](http://stackoverflow.com/q/336210/781965) – Jeff May 01 '13 at 08:33
-
my string should contain atleast one character and one alphabet – neethu May 01 '13 at 08:47
-
1That's something you should mention in your question, not just in a comment. Not everyone reads those. Also, define "alphabets" and "numbers". Does `ä` count? – Tim Pietzcker May 01 '13 at 09:43
-
What do you mean by `one character and one alphabet`? – Toto May 01 '13 at 10:19
3 Answers
Description
This will
- find all strings which have at least one letter and one number
- and only contain letters and numbers in any order
^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*$
Example
Note for some of these examples I have removed the $
at the end of the expression as source string in the example really contains many lines. To properly validate the string you'll need to remove the multiline matching and use the $
character as shown above.
Sample Text
11
22
33
1
2
3
1a
2b
3c
a1
b2
c3
1a1a
2b2b
3b3b
1a1
2b2
3b3
a1a
b2b
c3c
a
b
c
aa
bb
cc
C# Code Example
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = "source string to match with pattern";
Regex re = new Regex(@"^(?=[^\s]*?[0-9])(?=[^\s]*?[a-zA-Z])[a-zA-Z0-9]*",RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection mc = re.Matches(sourcestring);
int mIdx=0;
foreach (Match m in mc)
{
for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
{
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
}
mIdx++;
}
}
}
}
Matches Found
[0][0] = 1a
[1][0] = 2b
[2][0] = 3c
[3][0] = a1
[4][0] = b2
[5][0] = c3
[6][0] = 1a1a
[7][0] = 2b2b
[8][0] = 3b3b
[9][0] = 1a1
[10][0] = 2b2
[11][0] = 3b3
[12][0] = a1a
[13][0] = b2b
[14][0] = c3c
Summary
This works because the expression looks ahead to validate it can indeed find a number and a letter in the string, then matches all letters and numbers
In order to be unicode compatible:
^[\pL\pN]+$
\pL
stands for any letter
\pN
stands for any digit

- 89,455
- 62
- 89
- 125
To match just a single 0-9 or a-z or A-Z.
[0-9a-zA-Z]
To match 1 or more
[0-9a-zA-Z]\+
And if you are interested in the entire line being alphanumeric characters.
^[0-9a-zA-Z]\+$
The ^ matches the beginning of the line and the $ matches the end of the line.

- 626
- 8
- 20
-
Just in case it's useful to the OP, you can use `*` to denote 0 or more (if the empty string is acceptable) – Jeff May 01 '13 at 08:40
-
-