意外的正则表达式结果

发布于 2024-12-12 11:36:14 字数 731 浏览 4 评论 0原文

我见过几个关于如何检索和删除分隔符之间的字符串的示例。但是,我创建了一个非常简单的测试用例,并且根据各种示例得到了错误的输出。为什么我返回的是我想要用作搜索源的字符串而不是我正在搜索的字符串?

String temp = "56.0 F (13.3C)";
String exp = "((^))";
String p = temp.replace(exp, "");
System.out.println("p=" + p); 

我得到字符串 56.0F (13.3C) 作为输出。

我期待返回 56.0F

我还尝试使用模式匹配器:

Pattern pattern = Pattern.compile(exp);
Matcher matcher = pattern.matcher(temp);
if (matcher.find()) {
    System.out.println(matcher.groupCount());
    for (int i=0; i<=matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
        System.out.println(groupStr);
    }
}

2 是打印到控制台的内容

关于我做错了什么有什么想法吗?我现在已经看了几个小时了。谢谢!

I've seen several examples on how to retrieve and remove strings between delimiters. However, I created a very simple test case and I'm getting the wrong output based on various examples. Why am I getting back the string I'd like to use as the search source instead of the string I'm searching?

String temp = "56.0 F (13.3C)";
String exp = "((^))";
String p = temp.replace(exp, "");
System.out.println("p=" + p); 

I get the string 56.0F (13.3C) as my output.

I am expecting to get 56.0F back.

I also tried to use the pattern matcher:

Pattern pattern = Pattern.compile(exp);
Matcher matcher = pattern.matcher(temp);
if (matcher.find()) {
    System.out.println(matcher.groupCount());
    for (int i=0; i<=matcher.groupCount(); i++) {
        String groupStr = matcher.group(i);
        System.out.println(groupStr);
    }
}

2 is what's printed to the console

Any ideas as to what I'm doing wrong? I've been looking at this now for a couple of hours. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2024-12-19 11:36:15

您的第一个示例会打印原始字符串,因为 replace() 方法不执行正则表达式替换。它试图替换文字字符串 ((^)),这当然不存在。

但是,如果您使用 replaceAll()replaceFirst(),您会得到相同的结果。正则表达式中的 ^ 是零宽度断言;它不消耗任何字符,它只是将匹配锚定到字符串的开头。我希望从第二个示例中看到的结果是

> 56.0F (13.3C)
>
>

换句话说,整个字符串 (group(0)) 和两个空字符串 (group(1) 和 <代码>组(2))。这是因为两组括号都被解释为捕获组。如果您想匹配(字面的)括号及其内容,您可以使用:

String exp = "\\(.*?\\)";

...或者甚至更好:

String exp = "\\([^()]*\\)";

根本不需要使用捕获组。

Your first example prints the original string because the replace() method doesn't perform a regex replacement. It's trying to replace the literal string ((^)), which of course is not present.

However, you would have gotten the same result if you had used replaceAll() or replaceFirst(). The ^ in your regex is a zero-width assertion; it doesn't consume any characters, it just anchors the match to the beginning of the string. The result I would expect to see from your second example is

> 56.0F (13.3C)
>
>

In other words, the whole string (group(0)) and two empty strings (group(1) and group(2)). That's because both sets of parens are interpreted as capturing groups. If you want to match the (literal) parentheses and their contents, you can use:

String exp = "\\(.*?\\)";

...or even better:

String exp = "\\([^()]*\\)";

There's no need to use capturing groups at all.

A君 2024-12-19 11:36:14

它正在返回字符串,因为它无法进行任何替换。

尝试使用 \(.*?\)\([^)]*\) 来匹配由括号包围的字符串部分。

如果您希望它选择性地吃掉周围的空白,请在正则表达式的两侧放置一个 \s* ,或者对结果进行修剪。

It is returning the string because it could not do any replacements.

Try using \(.*?\) or \([^)]*\) to match the portion of the string surrounded by parenthesis inclusive.

If you want it to optionally eat up surrounding whitespace, put a \s* either side of the regex, or do a trim on the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文