Getting started with RegEx (1)

Source: Internet
Author: User
RegEx Getting started (1)

 

Regular Expressions usually have a lot of contact, but most of them are in the form of casual flowers. If you want to search, you will forget to read it. Today, I reviewed the results and wrote them out to my summary:

 

The following is a simple usage. The complexity will continue tomorrow:

LRegEx. ismatchUsage

 //Simply match a word

Console. writeline ( " \ N \ nismatch Demo: " );
RegEx Reg =   New RegEx ( " Aaron " );
Console. writeline ( String . Format ( " Result1: {0} " , Reg. ismatch ( " My name is Aaron. " ))); // True
Console. writeline ( String . Format ( " Result2: {0} " , Reg. ismatch ( " My name is Arron. " ))); // False

 

 //The default value is case sensitive, so the following two results will return different results.

Console. writeline ( String . Format ( " Result3: {0} " , RegEx. ismatch ( " My name is Aaron. " , " Aaron " ))); // False
Console. writeline ( String . Format ( " Result4: {0} " , RegEx. ismatch ( " My name is Arron. " , " Aaron " ))); // False

 

 //This is case insensitive.

Console. writeline ( String . Format ( " Result3: {0} " , RegEx. ismatch ( " My name is Aaron. " , " Aaron " , Regexoptions. ignorecase ))); // True

 

 

LRegEx. ReplaceUsage

Simply replace a word

 

// Simply replace a word
Console. writeline ( " \ N \ nreplace Demo: " );
Reg = New RegEx ( " R " );
Console. writeline ( String . Format ( " Result4: {0} " , Reg. Replace ( " My name is Arron. " , " R " ))); // My name is Arron.
Console. writeline ( String . Format ( " Result4: {0} " , Reg. Replace ( " My name is Arron. " , " R " , 1 ))); // Replace only once // My name is Arron.

 

 

 

LRegEx. MatchUsage

 //* ********************* Match usage **************** *************

Console. writeline ( " \ N \ nmatch Demo: " );
Reg =   New RegEx ( " Aa... " ); // The first two letters must be AA and followed by any 3 Characters
Match m = Reg. Match ( " My name is Aaron, Aaron, Aaron " );
While (M. Success)
{
Console. writeline (M. value );
M = M. nextmatch ();
// The default value is case sensitive.
// Aaron is a match.
// Aaron is also a match
// Aaron won't match
}

 

 

LRegEx. matchsUsage

// ******************** ***
Console. writeline ( " \ N \ nmatchscollection Demo: " );
Matchcollection MC = RegEx. Matches ( " My name is Aaron, Aaron, Aaron " , " Aa... " ); // The first two letters must be AA and followed by any 3 Characters

console. writeline ( string . format ( " found: {0} " , MC. count);

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.