Regular Expression in C #
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
May be interested
- Handling exceptions (Try / Catch / Finally) in C #an exception is an issue that occurs during the execution of a program. an exception in c # is a response to an exception situation that occurs while a program is running, such as dividing by zero.
- I / O file in C #a file is a collection of data stored on the drive with a specific name and a directory path. when a file is opened for reading or writing, it becomes a stream.
- Attribute in C #the attribute in c #, is a declaration tag, used to transmit information to the runtime about the behavior of various elements such as classes, methods, structures, enum, assemblies, etc. in the program. yours. you can add declaration information to the program using attribute.
- Reflection in C #reflection objects are used to obtain type information at runtime. these classes provide access to the program's metadata running in the system.reflection namespace in c #.
- Indexer in C #indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
- Delegate in C #delegate in c # is similar to pointers to functions, in c or in c ++. delegate is a reference type variable that contains references to a method. that reference can be changed at runtime.