Regular Expressions containing Chinese characters, numbers, letters, and underscores

Source: Internet
Author: User

We often encounter verification characters or Chinese Characters in ASP. NET. We searched for some good examples and solutions on the Internet and listed them for sharing with you.

You can use a regular expression to verify Chinese characters, numbers, and characters.

A method found on the internet is provided:

Source: http://www.cnblogs.com/flashicp/archive/2007/06/13/782355.html

1. A regular expression that only contains Chinese characters, numbers, letters, and underscores. It cannot start or end with or after the following line:
^ (?! _)(?!. *? _ $) [A-zA-Z0-9 _/u4e00-/u9fa5] + $ where:
^ Match the start point of the string
(?! _) Cannot start _
(?!. *? _ $) Cannot end _
[A-zA-Z0-9 _/u4e00-/u9fa5] + at least one Chinese character, number, letter, underline
$ Match the end of the string

Add @ in front of the program; otherwise, escape @ "^ (?! _)(?!. *? _ $) [A-zA-Z0-9 _/u4e00-/u9fa5] + $"
(Or: @ "^ (?! _)/W *(? <! _) $ "Or @" ^ [/u4e00-/u9FA50-9a-zA-Z _] + $ ")

2. It only contains Chinese characters, numbers, letters, and underscores. The underline is not limited to any position:
^ [A-zA-Z0-9 _/u4e00-/u9fa5] + $

3. A string consisting of digits, 26 English letters, or underscores
^/W + $

4, 2 ~ 4 Chinese Characters
@ "^ [/U4e00-/u9fa5] {2, 4} $ ";

5,
^ [/W-] + (/. [/W-] +) * @ [/W-] + (/. [/W-] +) + $

Analysis with :( ABC) +: xyzabcabcabcxyzabcab

XYZAbcAbcAbcXYZAbcAb

6,
[^/U4e00-/u9FA50-9a-zA-Z _]
34555 # 5' --> 34555#5'

[/U4e00-/u9FA50-9a-zA-Z _] eiieng_89 _ ---> eiieng_89 _
_ '; 'Eieng _ 88 & * 9 _ --> _'; 'eieng _ 88 & * 9 _
_ '; 'Eieng _ 88 _ & * 9 _ --> _'; 'eieng _ 88 _ & * 9 _

Public bool regexname (string Str)
{
Bool flag = RegEx. ismatch (STR, @ "^ [a-zA-Z0-9 _/u4e00-/u9fa5] + $ ");
Return flag;
}

RegEx Reg = new RegEx ("^ [a-zA-Z_0-9] + $ ");
If (Reg. ismatch (s ))
{
// Meets the rules
}
Else
{
// Invalid characters exist
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.