正则表达式 - 组值替换

发布于 2024-09-28 04:39:45 字数 197 浏览 4 评论 0原文

我不确定这是否可行,但一旦匹配,我需要一种方法来用运行时动态声明的字符串替换正则表达式表达式中指定的编号组的值。

给定一个简单的情况,比如...

(/)?([A-Za-z0-9])?(/)?$

我希望能够插入第 2 组的替代品。

我目前正在使用 Java 的 Matcher 类。

I am not sure if this is possible to do, but I need a way to replace a value of a numbered group specified in the my regex expression with a string declared dynamically at runtime, once a match has been made.

Given a simple case, something like...

(/)?([A-Za-z0-9])?(/)?$

I would want to be able to plugin a replacement for group 2.

I am currently using Java's Matcher class.

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

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

发布评论

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

评论(3

手心的温暖 2024-10-05 04:39:46

我不确定这是否可行...

是的,这是可能的。请参阅下面的示例。

我希望能够插入第 2 组的替代品。

此演示“插入”第 2 组的 .toUpperCase 版本作为替代品。

import java.util.regex.*;

class Main {
    public static void main(String... args) {
        String input = "hello my name is /aioobe/ and I like /patterns/.";
        Pattern p = Pattern.compile("(/)([A-Za-z0-9]+)(/)");
        Matcher m = p.matcher(input);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String rep = m.group(1) + m.group(2).toUpperCase() + m.group(3);
            m.appendReplacement(sb, rep);
        }
        m.appendTail(sb);
        System.out.println(sb);
    }
}

印刷:

hello my name is /AIOOBE/ and I like /PATTERNS/.

I am not sure if this is possible to do...

Yes, it's possible. See the example below.

I would want to be able to plugin a replacement for group 2.

This demo "plugs in" the .toUpperCase version of group 2 as a replacement.

import java.util.regex.*;

class Main {
    public static void main(String... args) {
        String input = "hello my name is /aioobe/ and I like /patterns/.";
        Pattern p = Pattern.compile("(/)([A-Za-z0-9]+)(/)");
        Matcher m = p.matcher(input);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String rep = m.group(1) + m.group(2).toUpperCase() + m.group(3);
            m.appendReplacement(sb, rep);
        }
        m.appendTail(sb);
        System.out.println(sb);
    }
}

Prints:

hello my name is /AIOOBE/ and I like /PATTERNS/.
不顾 2024-10-05 04:39:46

是的,这是可行的。查看我对这个问题的回答,了解具体方法。事实上,这个问题可能应该作为重复问题来关闭。

您需要稍微更改一下正则表达式。我无法确切地告诉你要做什么,所以我不能给出任何细节,但至少你应该将所有这些问号移到组内。

(/)?([A-Za-z0-9])?(/)?$  // NO

(/?)([A-Za-z0-9]?)(/?)$  // YES

但它仍然会匹配目标字符串末尾的空子字符串,因为除了锚点 $ 之外,所有内容都是可选的。这真的是你想要做的吗?

Yes, that's doable. Check out my answer to this question to see how. In fact, this question probably should be closed as a duplicate.

You'll need to change the regex a little. I can't tell exactly what you're trying to do, so I can't give any specifics, but at the very least you should move all those question marks inside the groups.

(/)?([A-Za-z0-9])?(/)?$  // NO

(/?)([A-Za-z0-9]?)(/?)$  // YES

But it will still match an empty substring at the end of the target string, because everything is optional except the anchor, $. Is that really what you meant to do?

清晰传感 2024-10-05 04:39:46

返回正则表达式搜索的值并将其保存到变量中,然后使用正则表达式搜索结果作为查找目标并使用动态声明的字符串作为替换来对主字符串进行替换。

真正简化的概念:

String testString = "Hello there";
//String substring = *Do your regex work here*
if(substring.length() > 0) {
   testString.replace(substring, dynamicallyGeneratedString); 
}

Return the value of your regex search and save it to a variable, then do a replace on your main string using your regex search results as a find target and your dynamically declared string as a replacement.

Really simplified conceptual:

String testString = "Hello there";
//String substring = *Do your regex work here*
if(substring.length() > 0) {
   testString.replace(substring, dynamicallyGeneratedString); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文