简体   繁体   English

在Android中使用正则表达式处理字符串

[英]Use regular expressions process string in Android

I obtain a response by HttpGet. 我得到HttpGet的响应。 After getEntity().getContent() I get the code of HTML page, and then convert this page to String pageHTML. 在getEntity()。getContent()之后,我获得了HTML页面的代码,然后将该页面转换为String pageHTML。

I need to use regular expression match the pageHTML and then obtain the result. 我需要使用正则表达式匹配pageHTML,然后获取结果。

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? 如果正则表达式只返回n个值,如何创建?

Create your regular expressions using a Pattern . 使用Pattern创建正则表达式。 Then you can call pattern. matcher(pageHTML) 然后,您可以调用pattern. matcher(pageHTML) pattern. matcher(pageHTML) to get a Matcher . pattern. matcher(pageHTML)以获得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. Matcher可以让你知道是否有任何matchesfind是有一个下一场比赛,并采取group代表最后一场比赛的部分序列。

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 (..)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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