Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the regular expressions commonly used in .net

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the regular expressions commonly used in .net". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the regular expressions commonly used in .net"?

1. Matching string

Regular 1

/ / regular 1 Regex r = new Regex ("abc"); / / define a Regex object instance Match m = r.Match ("123abc456"); / / match if (m.Success) {/ / match} / / regular 2 r = new Regex ("(abc) *") in the string M = r.Match ("bcabcabc"); if (m.Success) {/ / match}

Regular 2

/ / string matching string line = "ADDR=1234;NAME=ZHANG;PHONE=6789"; Regex reg = new Regex ("NAME= (. +);"); / / for example, I want to extract the name value Match match = reg.Match (line) from line; string value = match.Groups [1] .value; Console.WriteLine ("value value is: {0}", value) 2. String replacement / / for example, I want to change the NAME value in the record in the following format to WANG string line = "ADDR=1234;NAME=ZHANG;PHONE=6789"; Regex reg = new Regex ("NAME= (. +);"); string modified = reg.Replace (line, "NAME=WANG;"); / / the modified string is ADDR=1234;NAME=WANG PHONE=6789 3. Match / / Match / / text contains "speed=30.3mph". You need to extract the speed value, but the unit of speed may be metric or imperial, and mph,km/h,m/s may be possible. In addition, there may be spaces before and after. String line = "lane=1;speed=30.3mph;acceleration=2.5mph/s"; Regex reg = new Regex (@ "speed\ value =\ s * ([\ d\.] +)\ s * (mph | km/h | mUnix s) *"); Match match = reg.Match (line); / / then match.Groups [1] .value will contain values in the returned results, while match.Groups [2] .value will contain units. Var value = match.Groups [1] .value; / / it is convenient to demonstrate here that do not use Chinese naming variable var unit = match.Groups [2] .Value; Console.WriteLine ("the value of speed is: {0} speed unit is: {1}", value, unit); fourth, extract the value of [] string pattern1 = @ "(? is) (?. *? |") / / (the version circulated on the Internet is so bad that the above version is only partial, and it is still powerless to deal with complex nested tags) / / the regular expression Regex reg = new Regex (@ "^\ s* |\ sblank $or (^\ s*) | (\ sblank $)") / / (a very useful expression that can be used to delete white space characters (including spaces, tabs, page feeds, etc.) at the beginning and end of a line) / / Tencent QQ number Regex reg = new Regex (@ "[1-9] [0-9] {4,}") / / (Tencent QQ number starts from 10000) / / China postal code Regex reg = new Regex (@ "[1-9]\ d {5} (?!\ d)"); / / (China postal code is 6 digits) / / IP address Regex reg = new Regex (@ "\ d +\.\ d +") / / (useful when extracting IP addresses) / / IP address Regex reg = new Regex (@ "((?: (?) 25 [0-5] | 2 [0-4]\ d | [01]?\ d?\ d)\\.) {3} (?: 25 [0-5] | 2 [0-4]\ d | [01]?\ d?\ d)") Thank you for reading, the above is the content of "what are the regular expressions commonly used in .net". After the study of this article, I believe you have a deeper understanding of the regular expressions commonly used in .net. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report