Reprinted usage of the RegEx. Matches static method for Regular Expressions

Source: Internet
Author: User
// ① Regular Expression => match string text = @ "this is a book, this is my book, is not IIS"; // define a pattern string, it is not just plain text, but also a regular expression string pattern = "is"; matchcollection matches = RegEx. matches (text, pattern, regexoptions. ignorecase | // ignore case-insensitive regexoptions. explicitcapture | // improves the retrieval efficiency. regexoptions. righttoleft // match the string from left to right); console. writeline ("matching string from right to left:"); foreach (match nextmatch in matches) {console. write ("matched position: {0, 2} ", nextmatch. index); console. write ("matched content: {0, 2}", nextmatch. value); console. write ("/N");} console. writeline (); // ② match start with uppercase I // "/B" is an escape sequence, representing the beginning and end (the boundary of a word, ignoring white space or punctuation) pattern = @ "/Bi"; matches = RegEx. matches (text, pattern, regexoptions. explicitcapture // improves the retrieval efficiency); console. writeline ("matching string from left to right:"); foreach (match nextmatch in matches) {console. write ("matched location: {0}", nextmatch. index); console. write ("matched Content: {0} ", nextmatch. value); console. write ("/N");} console. writeline (); // ③ match a string that starts with uppercase I and ends with uppercase S. // "/B" is an escape sequence that represents the start and end of a word (the boundary of a word, ignore blank or punctuation) // s * match any character that is not blank pattern = @ "/Bi/S * s/B"; matches = RegEx. matches (text, pattern, regexoptions. explicitcapture // improves the retrieval efficiency); console. writeline ("matching string from left to right:"); foreach (match nextmatch in matches) {console. write ("matched location: {0}", nextmatch. index); console. write ("matched content: {0}", ne Xtmatch. value); console. write ("/N");} console. writeline (); // ④ match his or IIS, where case-insensitive pattern = @ "[H | I] is"; matches = RegEx. matches (text, pattern, regexoptions. ignorecase | // ignore case-insensitive regexoptions. explicitcapture // improves the retrieval efficiency); console. writeline ("matching string from left to right:"); foreach (match nextmatch in matches) {console. write ("matched location: {0}", nextmatch. index); console. write ("matched content: {0}", nextmatch. value); console. write ("/ N ");} console. writeline (); // ⑤ the URL grouping matches text = "http: // 192.168.0.1: 2008"; pattern = @ "/B (/S + ): // (/S + )(?: :(/S +)/B "; matches = RegEx. matches (text, pattern); console. writeline ("matching string from left to right:"); foreach (match nextmatch in matches) {console. write ("matched location: {0}", nextmatch. index); console. write ("matched content: {0}", nextmatch. value); console. write ("/N"); For (INT I = 0; I <nextmatch. groups. count; I ++) {console. write ("matched group {0 }:{ 1, 4}", I + 1, nextmatch. groups [I]. value); console. write ("/N") ;}} console. read ();

 

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.