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
- How to search Google everything with Clipman in Linuxyou can do this by combining the clipman application with a specially formatted url and a simple 'regular expression (regex)' formula '.
- Germany Set to Host First NFL Regular Season Gameas part of its ongoing efforts to expand its global reach, the nfl will see regular-season games take place in germany from 2022 to 2025. the move is the latest in a series of scheduled fixtures that will take place outside the us.
- Turn your ordinary television into a smart TV with 5 smart waysinstead of spending money on smart tvs, nowadays, families absolutely have the opportunity to experience the top entertainment content when turning regular tvs into smart tvs.
- Expression usage in PUBG Mobileexpression does not help you run better in pubg mobile but will give you more fun moments in the meantime.
- How to Turn Off a Regular Calculatorcan't turn off your calculator? many regular pocket calculators do not have an off button. instead, calculators are designed to automatically shut down after a few minutes of non-use. if you want to turn off the computer immediately, you can use some key combinations.
- Calculation expressions in Access 2016expressions in ms access can be understood similarly to formulas in excel. expressions can include operators, constants, functions, and identifiers.
- 5 Important Differences Between Fast Chargers and Regular Chargersmany people think all chargers are the same, but that assumption leads to hours of slow charging — until they learn about these 6 important differences that are often overlooked.
- How to Make a Regular Printer Wireless with a Wi Fi Routertoday's tipsmake will show you how to turn a regular printer into a wireless printer by connecting the device to a router. if this doesn't work, you can still connect the printer to a computer connected to the internet and share it with other computers on the network.
- What is Notepad ++? Compare Notepad ++ and regular Notepadon windows operating systems available for us, notepad software can edit code quickly. but notepad available does not have many features and support for users. in this article, which software tips will guide you about notepad ++? compare notepad ++ and regular notepad.
- Viber reaches 100 million regular usersviber has announced information that its service has reached 100 million regular users. also on this occasion, the company released a new version for computers running windows, os x and linux with many improvements in the logo image (sticker).