在Java调试模式(正则表达式)失败(Android版) [英] Debugging Pattern (regex) failure in in Java (Android)

查看:2790
本文介绍了在Java调试模式(正则表达式)失败(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一块code中,在一种情况下,但不是另一个工作正常,做一个模式匹配。在code是当前:

I'm doing a pattern match in a piece of code that works fine in one circumstance but not another. The code is currently:

DLVRYrx = Pattern.compile("(\\d+\\s\\p{Letter}+\\s\\d+)\\s(\\d+(?:\\.\\d+)?)\\s(\\d+)");
Log.d(TAG, "* Regex matched " + DLVRYrx.matcher("01 Jan 01 60.9876 1234").groupCount() + " groups"); // prints 3 as expected in logcat
for (int i=19; i<(fields-6); i++) {
    final String DATAstr = values[i];
    try {
        Matcher Dmatch = DLVRYrx.matcher(DATAstr);
        String data1 = Dmatch.group(0);
    } catch (IllegalStateException e) {
        Log.e(TAG, "! Text ["+DATAstr+"] didn't match regex");
    }
}

在code抛出的Dmatch.group(0)行IllegalStateException异常。从渔获的logcat线的输出是01年1月1日60.9876 1234如上

The code throws IllegalStateException on the Dmatch.group(0) line. The output of the logcat line from the catch is "01 Jan 01 60.9876 1234" as above.

在做数据文件我读了hexdump都显示空间是预期的空间和之前或我相匹配的文本后无杂散字符。在调试这有什么建议?

Doing a hexdump on the data file I'm reading in shows the spaces are spaces as expected and no stray characters before or after the text I'm matching. Any suggestions on debugging this?

我做了一个有点code的变化只是为了测试我的前pression本身,现在我更糊涂了。在循环中,我现在检查,看看是否该字符串模式首先匹配,然后通过编译版本运行它:

I've done a bit of a code change just to test my expression itself, and now I'm more confused. Within the loop, I'm now checking to see if the string matches the pattern first, then running it through the compiled version:

Pattern P = Pattern.compile(DLVRYrxStr);
if(!DATAstr.matches(DLVRYrxStr)) {
    Log.e(TAG, "[" + DATAstr + "] doesn't match regex");
    break;
}
Matcher Dmatch = P.matcher(DATAstr);

(?)

不幸的是,模式匹配,从而通过对P.matcher线下降,当我尝试读取第一个匹配的组线后抛出异常:

Unfortunately(?) the pattern does match, and thus falls through to the P.matcher line, the line after which throws an exception when I try to read the first matching group:

W/System.err( 1238): java.lang.IllegalStateException: No successful match so far
W/System.err( 1238):    at java.util.regex.Matcher.ensureMatch(Matcher.java:607)
W/System.err( 1238):    at java.util.regex.Matcher.group(Matcher.java:358)

解决方案是增加.matches()检查如下:

Solution is to add ".matches()" check as below:

Matcher Dmatch = DLVRYrx.matcher(DATAstr);
Dmatch.matches();
String data1 = Dmatch.group(0);

是的,我使用在我的code一个如果的声明,但离开它自由悬挂如上正常工作。

Yes, I'm using that in an 'if' statement in my code, but leaving it free-hanging as above works fine.

推荐答案

DLVRYrx.matcher(...)。groupCount()只是告诉你,有3匹配它从创建的模式组。

Your DLVRYrx.matcher(...).groupCount() is simply telling you that there are 3 matching groups in the pattern it was created from.

即。
(\\\\ D + \\\\ \\\\小号p {}信+ \\\\ \\\\小号D +)
(\\\\ D +(?:\\\\ D +)
(\\\\ D +)

您需要调用

matcher.matches()

matcher.lookingAt()

matcher.find()

此前试图让 matcher.group(0),因为这些方法的提示Java来解析字符串。

Prior to attempting to get matcher.group(0) since these methods prompt java to parse the string.

这篇关于在Java调试模式(正则表达式)失败(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆