作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 ((?:[0-9]{1,3}[\.,]?)*[\.,]?[0-9]+)
进行过滤在 java 上以字符串形式输出价格,所以我将它们放在这样的位置:
public static final String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
if (m.matches()) {
Log.d(TAG, "found! good start");
if (m.groupCount() == 1) {
Log.d(TAG, "start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
Log.d(TAG, "start match price" + " : " + m.group(1));
}
}
我得到了样本 http://www.regexr.com/但它从未在运行时找到匹配项。有什么想法吗??
最佳答案
而不是使用 matches()
你应该运行 m.find()
它搜索下一个匹配项(这应该在 while
循环中完成!):
String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
String label = "$500.00 - $522.30";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
while (m.find()) {
System.out.println("found! good start");
if (m.groupCount() == 1) {
System.out.println("start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
System.out.println("start match price" + " : " + m.group(1));
}
}
输出
found! good start
start match price : 500.00
found! good start
start match price : 522.30
关于java - 过滤掉Java中的价格或成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824180/
我正在编写一个快速的 preg_replace 来从 CSS 中删除注释。 CSS 注释通常有这样的语法: /* Development Classes*/ /* Un-comment me for
使用 MySQL,我有三个表: 项目: ID name 1 "birthday party" 2 "soccer match" 3 "wine tasting evening" 4
我是一名优秀的程序员,十分优秀!