Regular Expression in C #
A regular expression is a pattern that can be matched to an input text. The .Net Framework provides a regular expression that allows matching like that. In C #, a pattern consists of one or more character constants, operators, or construct.
R expressionular is a pattern that can be matched to an input text. The .Net Framework provides a regular expression tool that allows such matching. In C #, a pattern consists of one or more character constants, operators, or construct.
There are many different types of characters, operators and constructs that help you define Regular Expressions in C #:
- Character escape
- Character class
- Anchor
- Grouping construct
- Quantifier
- Backreference construct
- Alternation construct
- Substitution
- Miscellaneous constructs
Construct to define Regular Expression in C #
- Character escape in C #
- Anchor in C #
- Grouping construct in C #
- Character class in C #
- Quantifier in C #
- Backreference construct in C #
- Alternation construct in C #
- Substitution in C #
- Construct in C #
- Regex class in C #
Character escape in C #
Basically, Escape Character in C # is special characters. The backslash character () in a regular expression indicates that the character follows it: either a special character or should be interpreted according to each character.
Here are the Escape Character in C #:
Anchor in C #
Anchoring allows a match to succeed or fail depending on the current position in the sequence. Below are the anchor in C #:
Grouping construct in C #
Grouping Construct in C # describes the sub-expressions of a Regular Expression and captures substring in a input string. The following table lists the Grouping Construct in C #:
Character class in C #
A Character class in C # matches any character in a set of characters. Here are the Character classes in C #:
Quantifier in C #
Quantifier in C # determines how many instances of the previous element (which can be a character, a group, or a Character class) must be present in the input string for a match to occur.
Backreference construct in C #
Backreference construct in C # allows a previously matched sub-expression to be defined next in the same Regular Expression.
This is a list of these constructs in C #:
Alternation construct in C #
The alternation construct in C # modifies a Regular Expression to enable yes / no action matching. The following table is a list of Alternation constructs in C #:
Substitution in C #
Substitution in C # is used in alternate patterns. Table below lists the Substitution in C #:
Construct in C #
The table below lists the construct mixes in C #:
Regex class in C #
The Regex class in C # is used to represent a Regular Expression. It has the following commonly used methods:
Only whether or not Regular Expression given in this Regex constructor finds a match in the specified input string.
2 public bool IsMatch (string input, int startat)Only that whether or not Regular Expression is given in this Regex constructor finds a match in the specified input string, starting at the given startat in the string.
3 public static bool IsMatch (string input, string pattern)Only whether or not Regular Expression has found a match in the specified input string.
4 public MatchCollection Matches (string input)Search for the specified input string for all occurrences of a Regular Expression.
5 public string Replace (string input, string replacement)In a specified input string, replace all strings that match a Regular Expression pattern with a given replacement string.
6 public string [] Split (string input)Divide an input string into an array of sub-strings at a location defined by a Regular Expression pattern defined in the Regex constructor.
For a complete list of methods and properties, please read Microsoft Documentation about C #.
Example 1: Match words beginning with S
using System ; using System . Text . RegularExpressions ; namespace QTMCSharp { class Program { private static void showMatch ( string text , string expr ) { Console . WriteLine ( "Biểu thức: " + expr ); MatchCollection mc = Regex . Matches ( text , expr ); foreach ( Match m in mc ) { Console . WriteLine ( m ); } } static void Main ( string [] args ) { string str = "Sao hôm nay Sáng quá!" ; Console . WriteLine ( "So khớp các từ bắt đầu với 'S': " ); showMatch ( str , @ "bSS*" ); Console . ReadKey (); } } }
Compile and run the C # program you will get the following result:
Match words starting with 'S':
Expression: bSS *
Star
shining
Example 2: Match words starting with c and ending with m
using System ; using System . Text . RegularExpressions ; namespace QTMCSharp { class Program { private static void showMatch ( string text , string expr ) { Console . WriteLine ( "Biểu thức: " + expr ); MatchCollection a = Regex . Matches ( text , expr ); foreach ( Match b in a ) { Console . WriteLine ( b ); } } static void Main ( string [] args ) { string str = "Quản trị mạng chấm com" ; Console . WriteLine ( "So khớp từ bắt đầu với 'c' và kết thúc với 'm':" ); showMatch ( str , @ "bcS*mb" ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following output:
Match words starting with 'c' and ending with 'm':
Expressions: bcS * mb
dot
com
Example 3: Replace space (white space):
using System ; using System . Text . RegularExpressions ; namespace RegExApplication { class Program { static void Main ( string [] args ) { string input = " QTM chào bạn! " ; string pattern = "s+" ; string replacement = " " ; Regex rgx = new Regex ( pattern ); string result = rgx . Replace ( input , replacement ); Console . WriteLine ( "Chuỗi ban đầu: {0}" , input ); Console . WriteLine ( "Chuỗi đã thay thế khoảng trống: {0}" , result ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Original string: QTM hello!
The string has replaced the gap: QTM greeted you!
According to Tutorialspoint
Previous article: Preprocessing directive in C #
Next lesson: Handling exceptions (Try / Catch / Finally) in C #
You should read it
- Regular Expression in MongoDB
- Regular Expression (RegEx) in Python
- Regular Expression in PHP
- Regular Expression in Unix / Linux
- Regular Expression and RegExp in JavaScript
- Expression in AngularJS
- How to search Google everything with Clipman in Linux
- How to separate text strings by commas or spaces in Excel
- How to use the Match function in Excel
- Class (Class) in C #
- How to Manipulate Strings in Java
- How to use the grep command on Debian 10
Maybe you are interested
The best video editing apps for iOS and Android Zhang Pandemic - 8X long legs seduce Chairman Taobao The best video editing applications for iPhone Guide to making friendship anniversary videos on Facebook How to add background music to Instagram videos 20+ images show that roommates are always the most dangerous kid on the planet