C #-regular expressions, commonly used data parsing-happy Dragon Boat Festival,

Source: Internet
Author: User

C #-regular expressions, commonly used data parsing-happy Dragon Boat Festival,

The Dragon Boat Festival is just waiting for a few hours. I wish you a happy holiday.

Here we will share several common Regular Expression parsing data writing methods in C #, which are actually the Regex class. For the regular expression matching format, read the regular expression api documentation carefully, thank you.

Start:

1. query whether there is a string of "Order Number" Data

1 // matched object 2 var expl = "[{\" Order No. \ ": 2006, \" Price \ ": 888.90, \" order time \": \ "\", \ "payment status \": \ "paid \", \ "Payment time \"}, {\ "Order No. \": 2007, \ "Price \": 999.99, \ "order time \": \ "\", \ "payment status \": \ "paid \", \ "Payment time \": \ "\" },{ \ "Order No. \": 2008, \ "Price \": 999, \ "order time \": \ "\", \ "payment status \": \ "unpaid \", \ "Payment time \": \ "\"}] "; 3 4 // 1. query whether there is a string of "Order Number" data 5 var isExists = Regex. isMatch (expl, @ "price"); 6 Console. writeLine ("1. query whether there is a string of "Order Number" data "); 7 Console. writeLine (isExists );View Code

TIPS: The IsMatch method is used to determine whether a matching item exists. Simply enter the information to be searched in the method.

2. query a "price" (excluding decimal part)

1 // 2. query a "price" (excluding decimal part) 2 var m02 = Regex. match (expl, "\" Price \ ": \ d +"); 3 Console. writeLine ("\ n2. query a" price "(excluding decimal part)"); 4 Console. writeLine (m02.Value );View Code

TIPS: Match directly obtains the value of the matching item

3. query the "price" of an order (including the fractional part of the price)

1 // 3. query the "price" of an order (including the fractional part of the price) 2 var m03 = Regex. match (expl, "\" Price \ ": [^,] +"); 3 Console. writeLine ("\ n3. query the" price "of an order (including the fractional part of the price)"); 4 Console. writeLine (m03.Value );View Code

TIPS: use regular ^ to match data

4. query the "price" of all orders"

1 // 4. query all orders "price" 2 MatchCollection m04 = Regex. matches (expl, "\" Price \ ": [^,] +"); 3 Console. writeLine ("\ n4. query all order" Prices "); 4 foreach (Match m004 in m04) 5 {6 Console. writeLine (m004.Value); 7}View Code

TIPS: Matches obtains the matched set group.

5. query the "price" of all orders and output the price information

1 // 5. query all order "Prices" and output price information 2 MatchCollection m05 = Regex. Matches (expl, "\" Price \":(? <Price> [^,] +) "); 3 Console. writeLine ("\ n5. query all order" Prices ", output price information"); 4 foreach (Match m005 in m05) 5 {6 Console. writeLine (m005.Groups ["price"]. value); 7}View Code

Experience: use regular expressions (? <Price> xxxx) format to obtain the value of an item in the matching Group

6. query all order information and output information

1 // 6. query all order information and output information 2 MatchCollection m06 = Regex. Matches (expl, "\" Order No \":(? <Orderid> [^,] +), \ "Price \":(? <Price> [^,] +), \ "order time \":\"(? <Createtime> [^,] +) \ ", \" payment status \":\"(? <Paystatus> [^,] +) \ ""); 3 Console. writeLine ("\ n6. query all order information, output information"); 4 foreach (Match m006 in m06) 5 {6 7 8 Console. writeLine (@ "{0 }:{ 1 },{ 2 }:{ 3 },{ 4 }:{ 5 },{ 6 }:{ 7 }", 9 m006.Groups ["orderid"], m006.Groups ["orderid"]. value, 10 m006.Groups ["price"], m006.Groups ["price"]. value, 11 m006.Groups ["createtime"], m006.Groups ["createtime"]. value, 12 m006.Groups ["paystatus"], m006.Groups ["paystatus"]. value); 13}View Code

TIPS: Get the values of multiple items in the matching Group

7. Query "unpaid" order information

1 // 7. Query "unpaid" Order Information 2 MatchCollection m07 = Regex. Matches (expl, "\" Order No \":(? <Orderid> [^,] +), \ "Price \":(? <Price> [^,] +), \ "order time \":\"(? <Createtime> [^,] +) \ ", \" payment status \":\"(? <Paystatus> unpaid) \ ""); 3 Console. writeLine ("\ n7. query" unpaid "order information"); 4 foreach (Match m007 in m07) 5 {6 7 8 Console. writeLine (@ "{0 }:{ 1 },{ 2 }:{ 3 },{ 4 }:{ 5 },{ 6 }:{ 7 }", 9 m007.Groups ["orderid"], m007.Groups ["orderid"]. value, 10 m007.Groups ["price"], m007.Groups ["price"]. value, 11 m007.Groups ["createtime"], m007.Groups ["createtime"]. value, 12 m007.Groups ["paystatus"], m007.Groups ["paystatus"]. value); 13}View Code

TIPS: Set groups matching different conditions

8. case-insensitive matching results

1 // 8. case-insensitive matching result 2 var m08 = Regex. isMatch ("I love ZuGuo", "ZUGUO"); 3 Console. writeLine ("\ n8. case-insensitive matching result"); 4 Console. writeLine (m08 );View Code

TIPS: Regex is case sensitive by default.

9. Case Insensitive

1 // 9. ignore case 2 var m09 = Regex. isMatch ("I love ZuGuo", "ZUGUO", RegexOptions. ignoreCase); 3 Console. writeLine ("\ n9. case-insensitive"); 4 Console. writeLine (m09 );View Code

TIPS: Use RegexOptions to ignore case sensitivity

10. Match string information with spaces

1 // 10. match string information with spaces 2 var m10 = Regex. match ("happy", "[^ A] +"); 3 Console. writeLine ("\ n10. matching string information with spaces"); 4 Console. writeLine (m10.Value );View Code

Experience: Regular Expressions are really powerful. Haha

Code:

1 // C #-Regular Expression, commonly used data parsing-happy Dragon Boat Festival 2 3 // matching object 4 var expl = "[{\" Order No. \ ": 2006, \ "Price \": 888.90, \ "order time \": \ "\", \ "payment status \": \ "paid \", \ "Payment time \": \ "\" },{ \ "Order No. \": 2007, \ "Price \": 999.99, \ "order time \": \ "\", \ "payment status \": \ "paid \", \ "Payment time \"}, {\ "Order No. \": 2008, \ "Price \": 999, \ "order time \": \ "\", \ "payment status \": \ "unpaid \", \ "Payment time \": \ "\"}] "; 5 6 // 1. query whether the "Order Number" data string exists. 7 var isExists = Regex. IsMatch (expl, @ "price"); 8 Console. writeLine ("1. query whether there is a string of "Order Number" data "); 9 Console. writeLine (isExists); 10 11 // 2. query a "price" (excluding decimal part) 12 var m02 = Regex. match (expl, "\" Price \ ": \ d +"); 13 Console. writeLine ("\ n2. query a" price "(excluding decimal part)"); 14 Console. writeLine (m02.Value); 15 16 // 3. query the "price" of an order (including the fractional part of the price) 17 var m03 = Regex. match (expl, "\" Price \ ": [^,] +"); 18 Console. writeLine ("\ n3. query an order" price "(including the fractional part of the price)"); 19 Console. writeLine (m03. Value); 20 21 // 4. query all orders "price" 22 MatchCollection m04 = Regex. matches (expl, "\" Price \ ": [^,] +"); 23 Console. writeLine ("\ n4. query all order" Prices "); 24 foreach (Match m004 in m04) 25 {26 Console. writeLine (m004.Value); 27} 28 29 // 5. query all order "Prices" and output price information 30 MatchCollection m05 = Regex. matches (expl, "\" Price \":(? <Price> [^,] +) "); 31 Console. writeLine ("\ n5. query all order" Prices ", output price information"); 32 foreach (Match m005 in m05) 33 {34 Console. writeLine (m005.Groups ["price"]. value); 35} 36 37 // 6. query all order information, output information 38 MatchCollection m06 = Regex. matches (expl, "\" Order No \":(? <Orderid> [^,] +), \ "Price \":(? <Price> [^,] +), \ "order time \":\"(? <Createtime> [^,] +) \ ", \" payment status \":\"(? <Paystatus> [^,] +) \ ""); 39 Console. writeLine ("\ n6. query all order information, output information"); 40 foreach (Match m006 in m06) 41 {42 43 44 Console. writeLine (@ "{0 }:{ 1 },{ 2 }:{ 3 },{ 4 }:{ 5 },{ 6 }:{ 7 }", 45 m006.Groups ["orderid"], m006.Groups ["orderid"]. value, 46 m006.Groups ["price"], m006.Groups ["price"]. value, 47 m006.Groups ["createtime"], m006.Groups ["createtime"]. value, 48 m006.Groups ["paystatus"], m006.Groups ["paystatus"]. value); 49} 50 51 // 7. Query "unpaid" order information 52 MatchCollection m07 = Regex. Matches (expl, "\" Order No \":(? <Orderid> [^,] +), \ "Price \":(? <Price> [^,] +), \ "order time \":\"(? <Createtime> [^,] +) \ ", \" payment status \":\"(? <Paystatus> unpaid) \ ""); 53 Console. writeLine ("\ n7. query" unpaid "order information"); 54 foreach (Match m007 in m07) 55 {56 57 58 Console. writeLine (@ "{0 }:{ 1 },{ 2 }:{ 3 },{ 4 }:{ 5 },{ 6 }:{ 7 }", 59 m007.Groups ["orderid"], m007.Groups ["orderid"]. value, 60 m007.Groups ["price"], m007.Groups ["price"]. value, 61 m007.Groups ["createtime"], m007.Groups ["createtime"]. value, 62 m007.Groups ["paystatus"], m007.Groups ["paystatus"]. value); 63} 64 65 // 8. case-insensitive matching result 66 var m08 = Regex. isMatch ("I love ZuGuo", "ZUGUO"); 67 Console. writeLine ("\ n8. case-insensitive matching result"); 68 Console. writeLine (m08); 69 70 // 9. case-insensitive 71 var m09 = Regex. isMatch ("I love ZuGuo", "ZUGUO", RegexOptions. ignoreCase); 72 Console. writeLine ("\ n9. case-insensitive"); 73 Console. writeLine (m09); 74 75 // 10. match string information with spaces 76 var m10 = Regex. match ("happy", "[^ A] +"); 77 Console. writeLine ("\ n10. matching string information with spaces"); 78 Console. writeLine (m10.Value); 79 80 Console. readLine ();View Code

At this moment, the idea is: three-day holiday at the Dragon Boat Festival. It's really boring to be alone in the smog City in Beijing. Fortunately, dota1 is available. Come with friends, 11 (walking 3 x Ox)

 

Related Article

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.