Regular Expression application: delimiter

Source: Internet
Author: User

When writing a questionnaire, you may encounter a small problem of splitting strings. The webpage name is "Browse _" + survey number + "_" webpage code. html there are many ways to get the survey number and page number. For example, you can use string Split. Use regular expressions for analysis: the data is actually between two underlines and periods. Therefore, write a regular expression: _ \ d {1, }_\ d {1 }\. _ \ d {1, }_\ d {1 }\. meaning: _: Match underline \ d {1,}: Match one or more numbers \.: Indicates matching a dot. Because the DOT has a special meaning, the escape character slash is used. The overall meaning is to first match an underline, then match one or more numbers, then match the underline, then match one or more numbers, finally match a point number, and finally name the matched data, name \ d {1,} matched in the first position as surveyID, and \ d {1,} matched in the second place as pageIndex. Then, the regular expression is changed _(? <SurveyID> \ d {1 ,})_(? <PageIndex> \ d {1}) \. Test code:

 public static bool GetPageIndexSurveyID(string url, ref int surveyID, ref int pageIndex)     {         Regex reg = new Regex(@"_(?<surveyID>\d{1,})_(?<pageIndex>\d{1})\.");         if (reg.IsMatch(url))          {             Match m = reg.Match(url);             surveyID = int.Parse(m.Groups["surveyID"].Value);             pageIndex = int.Parse(m.Groups["pageIndex"].Value);         }         return true;     }     

 


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.