ITeye博客

处女apk纠结过的技术点<3>

ping8080 · 2011-02-28 11:57 · 移动开发 · Markdown

正则表达式

android下使用正则表达式,和java没有多大的区别,搬过来直接照样,主要的难点是匹配方式比较难下手:

我这个例子是分组取值解析一个html网页,需注意:正则表达式不能识别回车 和换行 所有要用空格替换掉.

public static String getUserId(String str){
  str = str.replaceAll(String.valueOf((char) 10), " ");//替换回车
  str = str.replaceAll(String.valueOf((char) 13), " ");//替换空格
  String regEx="var\\sbasicPanel\\s=\\snew\\sExt.Panel.*?patient.userId.*?value:'(.*?)'";//.*?代表0个以上的任意字符 ,?代表截断点
  Pattern pattern=Pattern.compile(regEx);
  Matcher matcher=pattern.matcher(str);
  if(matcher.find()){
   return matcher.group(1);//取第一个匹配的 若使group()的话 那是获取所有
  }
  return null;
 }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。