The magical uses of regular expressions--getting an array of __ regular expressions

Source: Internet
Author: User

Book recommendation *************************************************************************************


21.7 Yuan

26.9 Yuan

38.3 Yuan

23.9 Yuan

71.3 yuan

16.6 Yuan

*************************************************************************************************************** Recently encountered in the work of the more interesting strings, if not the regular expression to analyze will feel very laborious, if the use of regular expression analysis method will feel that the code is concise and quality.
Let's talk about the following process.

An existing string of characters has been processed by me

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

All categories below the-->//female channel
public static readonly String allsubsort = "{beauty, un1070801},{slimming, un1070802},{jewelry, un1070803},{apparel, un1070804},{accessories, UN1 070805}, "
+ "{Yoga, un1090102},{home, un10709},{decoration, un1070901},{gardening, un1070902},{love, un1070106},{couple, un1070101},{daughter-in-law, UN1070102}"
+ ", {childcare, un10703},{nutrition, un1100205},{cuisine, un107070204},{medicinal diet, un110080101},{health care, un1100304},{psychology, un1100107},{Gynecology, UN1100507}" ;
A look you will find that this string includes the corresponding relationship between the key values, how to get the value through the key, where "{}" inside the two can be used as a key.
What would you do if you didn't have to do it right?
......
Now I use the regular to do is very simple.
Please look at the code

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

-->///<summary>
Find the category code by category name
</summary>
<param name= "Subsortname" > Category name </param>
<returns> Classification code </returns>
public static string Findsubsortcodebyname (String subsortname)
{
String strret = "";
if (subsortname = null)
return strret;
Subsortname = Subsortname.trim ();
String pattern = "{" + Subsortname + ", (? <key>[^}]*)}";
Match match = Regex.match (allsubsort, pattern);
if (match!= null)
strret = match. groups["Key"]. Value;
return strret;
}
The pattern in the code is interesting,
When subsortname= "Beauty", pattern= "{beauty, (? <key>[^}]*)}".
The red place is the key, with "()" for a group, the code key in "?<key>" represents the group's key (this key is, of course, a glyph), followed by the value of the group.
You can pass strret = match. groups["Key"]. Value; to get.

How do I get all of the value?

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

-->///<summary>
Get all classification codes to + Connect
</summary>
<returns> All classification Codes </returns>
public static string Getallsubsortcode ()
{
String strret = "";
String pattern = "{[^,]*, (? <key>[^}]*)}";
MatchCollection matchs = regex.matches (allsubsort, pattern);
if (matchs!= null)
{
foreach (Match match in Matchs)
{
if (match!= null)
Strret + + + match. groups["Key"]. Value;
}
}
Return Strret.trim (' + ');
The reason is the same, but the pattern is a bit the same.

Complete code Download

Testing a very good tool to download

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.