简体   繁体   中英

Use regular expressions process string in Android

I obtain a response by HttpGet. After getEntity().getContent() I get the code of HTML page, and then convert this page to String pageHTML.

I need to use regular expression match the pageHTML and then obtain the result.

I have created the regular expression.

If regular expression just return a value, how to create? If regular expression just return n values, how to create?

Create your regular expressions using a Pattern . Then you can call pattern. matcher(pageHTML) pattern. matcher(pageHTML) to get a Matcher .

The Matcher allows you to know if there is any matches , find is there is a next match, and take the group representing the last match's sub-sequence.

You can use groups to receive multiple values from a regular expression. See this for details.

Pattern datePatt = Pattern.compile("([0-9]{2})/([0-9]{2})/([0-9]{4})");

Matcher m = datePatt.matcher(dateStr);
if (m.matches()) {
  int day   = Integer.parseInt(m.group(1)); // get values inside the first (..)
  int month = Integer.parseInt(m.group(2)); // get values inside the second (..)
  int year  = Integer.parseInt(m.group(3)); // get values inside the third (..)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM