Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to insert value like
string1.string2
string1_string2
string1 string2

my code is
C#
 Regex str = new Regex(@"[a-zA-Z0-9]+$");
string string1="hello";
string string2="codeproject";
 if (str.IsMatch(string1+".."+string2))
            {
                Console.WriteLine("yes");
            }
            else
                Console.WriteLine("no");

output is yes
Posted

1 solution

You only anchored the end of the pattern.
All this tests is that there is at least 1 alpha-numeric at the end of the test-string.
You probably wanted:
C#
Regex str = new Regex(@"^[a-zA-Z0-9]+$");

to pattern match the whole string.

I'm unclear about what you mean by:
I want to insert value like 
string1.string2
string1_string2
string1 string2

What does that have to do with the pattern?

[Edit: following comment below]
How about:
C#
Regex str = new Regex(@"^[a-zA-Z0-9]+[ \._][a-zA-Z0-9]+$");

Will take two alphanumeric sequences separated by space, dot or underscore.

[Edit #2]
If you want to also match just a single alphanumeric sequence as well as two separated sequences, make the separator and second sequence optional:
C#
Regex str = new Regex(@"^[a-zA-Z0-9]+(?:[ \._][a-zA-Z0-9]+)?$");
 
Share this answer
 
v6
Comments
Member 7909353 14-Mar-14 14:32pm    
I want to save name like above example only
Matt T Heffron 14-Mar-14 14:48pm    
Please see edited solution
Member 7909353 14-Mar-14 15:00pm    
thank you
Matt T Heffron 14-Mar-14 16:17pm    
You're welcome.
Can you please formally "Accept Solution" with the green button?
Member 7909353 20-Mar-14 2:55am    
I want
string1
string1.string2
string1_string2
string1 string2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900