C # Regular Expression Regex class usage,

Source: Internet
Author: User

C # Regular Expression Regex class usage,

I. C # Regular Expression symbol Mode

 

Character

Description

\

Escape Character, escape a character with special functions into a common character, or vice versa

^

Match the start position of the input string

$

End position of matching input string

*

Matches the previous zero or multiple subexpressions.

+

Match the previous subexpression one or more times

?

Matches the previous zero or one subexpression.

{N}

NIs a non-negative integer that matches the previousNSubexpression

{N,}

NIs a non-negative integer that matches at least the previousNSubexpression

{N,M}

MAndNAll are non-negative integers, whereN<=M, Minimum matchNTimes and most matchingMTimes

?

When this character is followed by other delimiters (*, + ,?, {N},{N,},{N,M}) The matching mode matches as few strings as possible.

.

Match any single character except "\ n"

(Pattern)

MatchPatternAnd obtain the matching

(? :Pattern)

MatchPatternBut do not get matching results

(? =Pattern)

Forward pre-query, in any matchPatternTo match the start of the string.

(?!Pattern)

Negative pre-query, in any MismatchPatternTo match the start of the string.

X|Y

MatchXOrY. For example, 'z | food' can match "z" or "food ". '(Z | f) ood' matches "zood" or "food"

[Xyz]

Character Set combination. Match any character in it. For example, '[abc]' can match 'A' in "plain'

[^Xyz]

Negative value character set combination. Match any character not included. For example, '[^ abc]' can match 'p' in "plain'

[A-z]

Matches any character in the specified range. For example, '[a-z]' can match any lowercase letter in the range of 'A' to 'Z '.

[^A-z]

Match any character that is not within the specified range. For example, '[^ a-z]' can match a value other than 'A '~ Any character in 'z''

\ B

Match A Word boundary. It refers to the position between words and spaces.

\ B

Match non-word boundary

\ D

Matches a number, which is equivalent to [0-9].

\ D

Match a non-numeric character, equivalent to [^ 0-9]

\ F

Match a newline

\ N

Match A linefeed

\ R

Match a carriage return.

\ S

Matches any blank characters, including spaces, tabs, and page breaks.

 

\ S

Match any non-blank characters

\ T

Match a tab

\ V

Match a vertical tab. Equivalent to \ x0b and \ cK

\ W

Match any word characters that contain underscores. Equivalent to ''[A-Za-z0-9 _]'

\ W

Match any non-word characters. Equivalent to '[^ A-Za-z0-9 _]'

 

Note:

In the regular expression, "\", "? "," * "," ^ "," $ "," + "," (",") "," | "," {"," [", And other characters have some special significance. If you need to use their original meanings, escape them, for example, if you want to have at least one "\" in the string, the regular expression should be written as follows: \ +.

 

2. To use a regular expression class in C #, add the following statement at the beginning of the source file:Using System. Text. RegularExpressions; Iii. Common RegEx Methods 1. Static Match MethodThe static Match method is used to obtain the continuous substring of the first matching mode in the source. The static Match method has two reloads:Regex. Match (string input, string pattern); Regex. Match (string input, string pattern, RegexOptions options ); Input and mode parameters of the first type of Overload  The second type of overload parameter represents the input, mode, and RegexOptions enumerated "by bit or" combination.The valid value of RegexOptions enumeration is: Complied indicates compiling this mode. CultureInvariant indicates that ECMAScript meets ECMAScript without considering the cultural background, this value can only be used with IgnoreCase, Multiline, and Complied. ExplicitCapture indicates that only the explicitly named group IgnoreCase is saved. IgnoreCase indicates that the input case-insensitive IgnorePatternWhitespace indicates that the non-escape spaces in the mode are, enable the annotation Multiline marked by # To indicate Multiline mode, and change the meaning of the metacharacters ^ and $. They can match the beginning and end of the line with None to indicate no setting, this enumeration item has no meaning. RightToLeft indicates scanning and matching from the right to the left. In this case, the static Match method returns the first matching Singleline from the right to the left, indicating the single row mode and changing the metacharacter. it can match line breaks. Note: Multiline and Singlelin can be used without ECMAScript. E. Singleline and Multiline are not mutually exclusive, but they are mutually exclusive with ECMAScript. 2. Static Matches MethodThis method is reloaded in the same way as the static Match method. A MatchCollection is returned, indicating the set of matching modes in the input. 3. Static IsMatch MethodThis method returns a bool. The reload format is the same as the static Matches. If the input Matches the pattern, true is returned. Otherwise, false is returned. It can be understood as: IsMatch method, whether the set returned by the return Matches method is null. Iv. RegEx instances 

1. String replacement

For example, I want to change the NAME value in the following format record to WANG

String line = "ADDR = 1234; NAME = ZHANG; PHONE = 6789 ";

Regex reg = new Regex ("NAME = (. + );");

String modified = reg. Replace (line, "NAME = WANG ;");

The modified string is ADDR = 1234; NAME = WANG; PHONE = 6789.

 

2. String Matching

For example, I want to extract the NAME value from the record just now.

Regex reg = new Regex ("NAME = (. + );");

Match match = reg. Match (line );

String value = match. Groups [1]. Value;

 

3. Match instance 3

The text contains "speed = 30.2mph". You need to extract the speed value. However, the unit of speed may be either in the metric or in the Imperial, mph, km/h, or m/s; in addition, there may be spaces before and after.

String line = "lane = 1; speed = 30.3mph; acceleration = 2.5mph/s ";

Regex reg = new Regex (@ "speed \ s * = \ s * ([\ d \.] +) \ s * (mph | km/h | m/s )*");

Match match = reg. Match (line );

In the returned results, match. Groups [1]. Value will contain numbers, while match. Groups [2]. Value will contain units.

 

4. For another example, to decode the gps GPRS string, you only need

Regex reg = new Regex (@ "^ \ $ uplmc, [\ d \.] *, [A | V], (-? [0-9] * \.? [0-9] +), ([NS] *), (-? [0-9] * \.? [0-9] +), ([EW] *),. * ");

You can obtain the longitude and latitude values, and dozens of lines of code are required before.

 

V. Description of the namespace of System. Text. RegularExpressions

 

The namespace contains eight classes, one enumeration, and one delegate. They are:

 

Capture: contains the results of a Match; CaptureCollection: the sequence of Capture; Group: the results of a Group record, inherited by Capture; GroupCollection: indicates the set Match of the Capture Group: the matching result of an expression is inherited by the Group; MatchCollection: a sequence of Match; MatchEvaluator: The delegate used to perform the replacement operation; Regex: an instance of the compiled expression. RegexCompilationInfo: provides the information that the compiler uses to compile a regular expression into an independent assembly. RegexOptions provides the Regex class that is used to set the enumerated value of a regular expression. It also contains some static methods: Escape: escape the escape characters in the regex in the string; IsMatch: If the expression Matches in the string, this method returns a Boolean value; Match: returns the instance of the Match; Matches: returns a series of Match methods. Replace: replaces a string with a matching expression. Split: returns a series of strings determined by the expression. Scape scape: does not escape characters in the string.


CC real name

The story of LULU and King Arthur and the real name Analysis of CC
According to a strong analysis on the Internet.
Although lancot appeared in the first season
But he never imagined it with Arthur.
Because it is the name of the body, I don't think much about it.

Now I saw R2. seeing the name of the Round Table knight, the king opened his eyes with a pair of geass.
Only then can we realize that the essence of ruuxiu is actually a variant of Arthur's story...
If this is true, the true name of c. c is clear ..

The introduction of the Royal Family Name "briannia" refers to a relatively old saying in Britain. The "British kings" One hundred and fifty years ago used the British annia, and this book refers to the story of King Arthur's round table knight, the number of Round Table Knights ranges from.
In this tutorial, the Round Table server has a minimum of 12

In the legend of King Arthur, lancelot is a knight who betrayed the king because he fell in love with the king.
And he is an orphan of a young father

Let's look at the name of zhuque Shu Mu.
Pivot means key or important.
Zhuque now seems to be no different from the species of birds that can survive the burning of death.
So: OK: zhumu zhuque is a young father who will betray with love as an excuse. It plays an important role in this film, and how can he play an undead role...

King Arthur disappeared after a duel with his son mordred (in fact, he died). The reason for the duel is that this son wants to gain a throne. Please note that, king Arthur knew that his son would have killed him shortly after his birth (a legend in the west) but did not kill him. Instead, he continued to raise him.

Ruuxiu's name is very interesting.
Japanese is written in Japanese.
However, the official western style is: Lelouch Lamperouge
Let's take a look at this sentence.
Le louche explains abnormal behavior in the eyes and greedy behavior in the eyes.
Lampe rouge is a red light or lamp.
The son who killed Arthur was named Mordred.
The split shows that Mord is related to death + Red
OK: Lu luxiu is an eye that emits abnormal red light and greedy character (every time I play tactics, I want to use all the methods, and the result is reversed by the enemy's strong role ), sending is spreading the message of death to the prince who targeted his father.

Kalian
The old surname "Red Moon" in Area 11 refers to ominous things. Normally, attackers like to mount the moon at night.
The last name of the empire is Stadtfield, which indicates the field of the municipal council.
Her name, Kallen, was directly translated into Karine or Hualien for reference by the translation team.
In fact, this word means that it is a verb to hold it tightly with claws.
Everyone should know the explanation of her body, that is, the spark of a huge explosion.

OK: So cailian is a very honorable and non-invasive entity in the empire. However, it is always carrying out ominous activities in the dark. It is a fire of character and is good at operating a body that can hold the enemy tightly with claws.

So what?
Name of c. c. As a key character
It will certainly be associated with her miserable situation.
And it must be in touch with Arthur's background.
Because c. c. once belonged to the holy kingdom of Britain and then escaped.
That is to say, Arthur got and lost things.
This is not a human thing for Arthur.
And it can be related to the letter C.

Then only Arthur's first sword is Calibur.
The other name is Sword of the Stone before King Arthur gets it.
Later, it was broken in a duel that does not conform to the spirit of the server guard.

Caliburia is highly likely to become a personal name.
Considering that c. c. is a girl, it may be changed
Caliburian or Clirburess
Because C. C.'s real name is five syllables (11 episodes in the first season)
And... the remaining full text>

CC real name

The story of LULU and King Arthur and the real name Analysis of CC
According to a strong analysis on the Internet.
Although lancot appeared in the first season
But he never imagined it with Arthur.
Because it is the name of the body, I don't think much about it.

Now I saw R2. seeing the name of the Round Table knight, the king opened his eyes with a pair of geass.
Only then can we realize that the essence of ruuxiu is actually a variant of Arthur's story...
If this is true, the true name of c. c is clear ..

The introduction of the Royal Family Name "briannia" refers to a relatively old saying in Britain. The "British kings" One hundred and fifty years ago used the British annia, and this book refers to the story of King Arthur's round table knight, the number of Round Table Knights ranges from.
In this tutorial, the Round Table server has a minimum of 12

In the legend of King Arthur, lancelot is a knight who betrayed the king because he fell in love with the king.
And he is an orphan of a young father

Let's look at the name of zhuque Shu Mu.
Pivot means key or important.
Zhuque now seems to be no different from the species of birds that can survive the burning of death.
So: OK: zhumu zhuque is a young father who will betray with love as an excuse. It plays an important role in this film, and how can he play an undead role...

King Arthur disappeared after a duel with his son mordred (in fact, he died). The reason for the duel is that this son wants to gain a throne. Please note that, king Arthur knew that his son would have killed him shortly after his birth (a legend in the west) but did not kill him. Instead, he continued to raise him.

Ruuxiu's name is very interesting.
Japanese is written in Japanese.
However, the official western style is: Lelouch Lamperouge
Let's take a look at this sentence.
Le louche explains abnormal behavior in the eyes and greedy behavior in the eyes.
Lampe rouge is a red light or lamp.
The son who killed Arthur was named Mordred.
The split shows that Mord is related to death + Red
OK: Lu luxiu is an eye that emits abnormal red light and greedy character (every time I play tactics, I want to use all the methods, and the result is reversed by the enemy's strong role ), sending is spreading the message of death to the prince who targeted his father.

Kalian
The old surname "Red Moon" in Area 11 refers to ominous things. Normally, attackers like to mount the moon at night.
The last name of the empire is Stadtfield, which indicates the field of the municipal council.
Her name, Kallen, was directly translated into Karine or Hualien for reference by the translation team.
In fact, this word means that it is a verb to hold it tightly with claws.
Everyone should know the explanation of her body, that is, the spark of a huge explosion.

OK: So cailian is a very honorable and non-invasive entity in the empire. However, it is always carrying out ominous activities in the dark. It is a fire of character and is good at operating a body that can hold the enemy tightly with claws.

So what?
Name of c. c. As a key character
It will certainly be associated with her miserable situation.
And it must be in touch with Arthur's background.
Because c. c. once belonged to the holy kingdom of Britain and then escaped.
That is to say, Arthur got and lost things.
This is not a human thing for Arthur.
And it can be related to the letter C.

Then only Arthur's first sword is Calibur.
The other name is Sword of the Stone before King Arthur gets it.
Later, it was broken in a duel that does not conform to the spirit of the server guard.

Caliburia is highly likely to become a personal name.
Considering that c. c. is a girl, it may be changed
Caliburian or Clirburess
Because C. C.'s real name is five syllables (11 episodes in the first season)
And... the remaining full text>

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.