请教高手, 比较难的正规表达式

javaglory 2010-12-01 10:28:56
请问,怎么用正则表达式把String s="<SPAN style="COLOR: #ff0000">268.28</SPAN>";

中抽取"三位整数外加两位小数"的数字268.88出来? 谢谢大家.
...全文
89 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaglory 2010-12-02
  • 打赏
  • 举报
回复

public static void main(String[] args) {
String s1="<SPAN style='COLOR: #008000'>28.28</SPAN>";
List<String> resultList = getRegExpValsList(s1,"(?is)color.+?>(?!10{2}\\s+)(?=[1-2]\\d{2}[\\d\\.]*)([\\d\\.]+)"); //关键我是不知怎么调这句正则表达式,才会打印28.28
for(String tmpS:resultList )
(
System.out.println(resultList);
}
}

public static List<String> getRegExpValsList(String regExp, String oriStr) {
List<String> rtn=new ArrayList<String>();

Matcher m=Pattern.compile(regExp).matcher(oriStr);
while(m.find()){
if(m.group(1)!=null)
{

rtn.add(m.group(1));
}
}
return rtn;
}
eXeSP 2010-12-02
  • 打赏
  • 举报
回复
要匹配的字符串中不能包含太多的" ",字符串不支持。

public static void main(String[] args) {
//String s="<SPAN style="COLOR: #ff0000">268.28</SPAN>";
String regex = "\\.*\\d{3}.\\d{2}\\.*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(">268.28<");
if(m.find()) {
System.out.println(m.group());
}

}
猿敲月下码 2010-12-02
  • 打赏
  • 举报
回复
如果格式固定不变的话可以这样

String s = "<SPAN style=\"COLOR: #ff0000\">268.28</SPAN>";
System.out.println(s.replaceAll("^.*(\\d{3}\\.\\d{2}).*", "$1"));
fantasy0126 2010-12-02
  • 打赏
  • 举报
回复
String s="<SPAN style=\"COLOR: #ff0000\">268.28</SPAN>";
Matcher m=Pattern.compile("(?i)<span.*?>(\\d*(\\.\\d.*)?)</span>").matcher(s);
if(m.find())
System.out.println(m.group(1).trim());
茫茫大海 2010-12-02
  • 打赏
  • 举报
回复
楼上的两位都说清楚了!
fantasy0126 2010-12-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fantasy0126 的回复:]

String s="<SPAN style=\"COLOR: #ff0000\">268.28</SPAN>";
Matcher m=Pattern.compile("(?i)<span.*?>(\\d*(\\.\\d.*)?)</span>").matcher(s);
if(m.find())
System.o……
[/Quote]
防止268.28前后出现空格
改一下
Matcher m=Pattern.compile("(?i)<span.*?>(\\s*\\d*(\\.\\d.*)?)</span>").matcher(s);

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧