Preface
Recently I learned C#’s window application development and programming, and then I searched the website for one morning, but most of them were able to understand it if I had some basic knowledge. For example, I was a novice, but I couldn’t understand it after reading it for a long time.
Finally, I asked the great god and taught me
Below is a summary of my regular expressions in C#. I wrote a blog for the first time. Hahahaha, please forgive me for not writing well. Dear guys, I love you.
1. The concept of regular expressions
What is a regular expression?
Regular Expression is an important concept in computer science. It uses a mathematical algorithm to solve problems such as text retrieval, matching, etc. in computer programs. Regular Expression Language is a language specifically used for string processing. It is supported in many languages, and C# is no exception. It can help us solve the following problems: such as account number, password verification, etc.
1.1 Composition of regular expressions
Regular expressions are text patterns composed of ordinary characters and special characters (becoming meta characters). This pattern describes searching for text
What are 1.1.1 element characters
The so-called metacharacter refers to those in regular expressionsIt has special significanceThe specific characters of , which can be used to specify the appearance pattern of its leading characters (that is, the characters that are before the metacharacter) in the target object.
1.1.2 Positioning metacharacters
Character Description
- \b Match the beginning or end of a word.
- \B Match the beginning or end of a non-word
- ^ Match must appear at the beginning of the string or at the beginning of the line
- $ matches must appear at the following locations: end of the string, before \n at the end of the string, or end of the line.
- \A Specifies that the match must appear at the beginning of the string (ignore the Mutiline option).
- \z specifies that the match must appear at the end of the string (ignore the Multiline option).
- \z specifies that the match must appear before \n at the end of the string or at the end of the string (ignore the Multilie option).
- \G Specifies that the match must appear at the end of the previous match. When used with() -, this assertion ensures that all matches are continuous.
1.2 Three brackets 😳
There are 3 types of brackets in regular expressions
They are: square brackets, curly brackets, round brackets, (abbreviated as square garden) 😁
Square brackets "[" and curly brackets "{"●
Square brackets "["inside isThe matching characters are required,
Braces "{" areSpecifies the number of matching characters.
Parentheses "(": mean to be used forGroupingof.
Carest "enter": means regularstart。
The dollar sign "$": represents a regularFinish。
1.3 Simplify regular expressions
Actual command | Quick Command |
---|---|
[0-9](number) | d |
[a-z][0-9][_](number, letter, underline) | W |
Occurs 0 or more times | * |
Occurs at least once | + |
Occurs 0 or 1 | ? |
1.4 The role of @ symbols
We often add @ characters to regular expression strings, with the purpose of not allowing the compiler to parse escaped characters, but exists as regular expression syntax (metacharacter).
Insert code slice here
1.5 Regular expressions can implement four functions:
Because I saw other blogs saying that C# has many ways to use regular expressions, and the following examples use the simpler methods I think. Master, don't criticize, ┗|O'|┛ Ao~~
Warm reminder: Use the following sentences to introduce
using Namespace
1. Get: Get the part we want from the string through regular expressions
Regex reg = new Regex("[0-9]*");//This is the search for numbers matching 0-9(("12asda"));//The final extracted 12 successful, hehehe
2. Match: determine whether the given character conforms to the filtering logic of regular expressions. You can think that regular expressions express the writing rules of a string.
Here you have to use the number from the beginning to the endTrue,An output appears for non-digitalFalse Regex reg = new Regex("^[0-9]*$");//Judge whether the entire string is a number(("12asda"));//False output last(("124536346"));//The final output is True
3. Split
1、Separate by string: using ; string str="aaajsbbbjsccc"; string[] sArray=(str,"js",); foreach (string i in sArray) (() + "<br>"); Output result: aaa bbb ccc
2、Separate with multiple characters: string str="aaajbbbscccjdddseee"; string[] sArray=(new char[2] {'j','s'}); foreach(string i in sArray) (() + "<br>"); Output result: aaa bbb ccc ddd eee
3、Separate with single characters: string str="aaajbbbjccc"; string[] sArray=('j'); foreach(string i in sArray) (() + "<br>"); Output result: aaa bbb ccc
4, Replace
string s = "aaa12342525"; Regex r = new Regex("[0-9]");// Regular expression rules(r);//This output is [0-9] (I don't know the specific principle either) 😭s = (s, "9", 1);//The first parameter is a string, the second is the character to be replaced when matching the string correctly, and the third parameter is the number of times the regular expression is correctly matched and then replaced.(s);//Output aaa92342525
2. Commonly used regular expression methods
2.1 Static method IsMath
1. Static method IsMatch
IsMath (the return value is a boolean type, used to determine whether the specified string matches the regular expression character, it hasThree overload methods)
bool IsMatch(string input, string pattern);
parameter:
input: The string to search for matches.
pattern: The regular expression pattern to match.
Returns the result: true if the regular expression finds a match; otherwise, false.
bool IsMatch(string inpdt, string pattern, RegexOptions options);
parameter:
input: The string to search for matches.
pattern: The regular expression pattern to match.
options: A bitwise combination of enum values, which provide matching options.
Returns the result: true if the regular expression finds a match; otherwise, false.
bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
parameter:
input: The string to search for matches.
pattern: The regular expression pattern to match.
options: A bitwise combination of enum values that provide matching options.
matchTimeout: Timeout
or
Indicates that the method should not time out.
Returns the result: true if the regular expression finds a match; otherwise, false.
2.1.1 About Parameters RegexOptions
It is an enum type with the following enum value
RegexOptions enumeration value Inline flag Simple description
ExplicitCapture n n n
IgnoreCase �
IgnorePatternWhitespace x Eliminate non-escaped whitespace in mode and enable comments marked by #.
MultiLine m
SingleLine
2.2 Static method Match
What Match does: Use the specified matching option to search for the first match of the specified regular expression in the input string. Returns an object containing information about the match. There are also three overloaded methods, the parameters and IsMatch method are the same.
In addition, in the Regex class, there is a non-static method with the same name, which is more efficient when multiple instances are applied - some.
The following three overloading methods for Match
- Match Match(string input, string pattern);
- Match Match(string input,string pattern, RegexOptions options); input,
- Match Match(string string pattern, RegexOptions options, TimeSpan matchTimeout);
2.3 Static methods Matchs
Static method Matches, search for the specified input stringAll matches to the specified regular expression. The difference from the above method is that this method returnsAll matches, it also has three overloaded methods, and the parameters and Match methods are exactly the same. In addition, in the Regex class, there is also a non-static method with the same name, which is more efficient when multiple instances are applied - some.
2.4 Replaces function
We know that regular expressions mainly implement the functions of validation, extraction, segmentation, and replacement characters. The Replace function implements the replacement function.
1 )Replace(string input,string pattern,string replacement) //input is the source character string, pattern is the matching condition, replacement is the replacement content, which is to convert the content that meets the matching condition pattern into it for examplestring result = ("abc", "ab", "#" ); ➢//The result is #c, is to replace the ab in the string abc with #2 )Replace(string input,string pattern,string replacement,RegexOptions options) //RegexOptions is - an enumeration type used to make - some settings //It was used when using comments before. If you ignore case when matching, you can use it for examplestring result = ("ABc", "ab", “#" ,);
Supplement: Use the IsMatch method in the Regex class to determine whether the matching string meets the requirements of regular expressions
class Program { static void Main(string[] args) { ("Please enter an email"); string email = (); Regex regex = new Regex(@"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); if ((email)) { ("The mailbox is in the correct format."); } else { ("The email format is incorrect."); } } }
Summarize
This is the end of this article about the usage of C# regular expressions (Regex class). For more related content on the usage of C# regular expressions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!