Book recommendation *************************************************************************************
*************************************************************************************************************** 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