Some used regular expressions

Source: Internet
Author: User
Tags first string
1. Remove header information from XML or similar files

string text = File.readalltext ("E:\\aa.xml"). Replace ("\ r \ n", ""); text = Regex.Replace (text, @ "<!--* *--", "", regexoptions.ignorecase);

Function:
Replace the carriage return, line break in the Aa.xml file with "", and replace the <!--*. * with "".
IgnoreCase (ignoring case) matches are case-insensitive.
ReadAllText open a text file, read all the lines of the file, and then close the file.

public static string ReadAllText (string path) path type: System.String the file to be opened for reading. return value  Type: System.String A string that contains all the rows of the file. public static string Replace string input//string pattern to be modified,//The regular expression pattern to match string replacement,//replace strings Regexoption s options//regexoption The bitwise OR combination of the enumeration values//returns the modified string

If you want to see the Replace function detailed code information, with reflector or ilspy open the System.dll, in the System.Text.RegularExpressions namespace to find the Regex class, you can.

2. verification of Identity card information

string reg = "^[0-9]{15,16}$";

Small bet

^ start of matching string
[...] Match all the characters listed in []
{n,m} matches the preceding characters n to M times
$ match end of string

3. Verify homepage, url

Regex.IsMatch (str, @ "^ (http://) {0,1}www. ( \w) +. (COM|NET|ORG|COM.CN|NET.CN|ORG.CN|GOV.CN|INFO|BIZ|TV|CC|CN) $ ");

Small bet

Where Str is the home page or URL to be verified

^ start of matching string
(and) marks the start and end of a sub-expression. Sub-expressions can be obtained for later use. To match these characters, use \ (and \).
{n,m} matches the preceding characters n to M times
/w Match letters or numbers or underscores or kanji
+ Repeat one or more times
() indicates the scope and priority of the operation, such as "GR (a|e) y", which matches gray or grey.
| Represents the selection symbol, "Gray|grey" matches gray or grey.
$ match end of string

4, Check whether the text or the first string is a number

Matches the regular expression, minus @ does not affect the effect of the regex r = new Regex (@ "^[0-9]");

Small bet

like the meaning of [0-9] represents exactly the same AS/ D .

5. Verification zip code

string reg = "^[0-9]{6}$";

Small bet

{n} matches the preceding character n times

6, verify the mailbox

string reg = "^[a-za-z][a-za-z0-9._]*@[a-za-z0-9." +[.] +[a-za-z0-9]+$ ";

Small bet

^ start of matching string
"@" means that the string following it is a "verbatim string", not very well understood, for example, the two declarations are equivalent:

String x= "D:\\my huang\\my doc"; string y = @ "D:\My huang\my doc";

In fact, C # will error if declared as follows, because "\" is used in C # for escaping, such as "\ n" Wrapping:

string x = "D:\My huang\my Doc";

* indicates the preceding character to appear 0 times, 1 times, or even many times, the above is not capped, the following guaranteed is 0 times, can not appear.
. match any character other than line break
+ Repeat one or more times
$ match end of string

7. Match integer

System.Text.RegularExpressions.Regex.IsMatch (str, @ "^-?\d+$")

Small bet

Where Str is the string type variable to be validated

^ start of matching string
? repeat 0 or one time
-? Repeat 0 or one time-
/ D match number
+ Repeat one or more times
\d+ Repeat one or more numbers
$ match end of string

Or:

^-? [1-9]\d*$

8. Detecting what numbers are in string strings

9. Take out the parts between two __

result = Regex.match (Abstractobjectentity.name, "(? <=_). =_)"). Value;

Reference: Click to open link

10.

These are some of the used regular expression content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.