简体   繁体   English

正则表达式替换替换字符串

[英]Regex replace the substitution string

Let's consider the following example: 让我们考虑以下示例:

String s = str.replaceAll("regexp", "$1");

Some languages allow us to specify \\U$1 in place of $1 which converts matched groups with uppercase letters. 某些语言允许我们指定\\U$1代替$1来转换匹配的组并使用大写字母。 How can I achieve the same using Java? 如何使用Java实现相同目的?

I know we can use Pattern class and get the group and convert it to uppercase, but that's not what I am looking for. 我知道我们可以使用Pattern类并获取组并将其转换为大写,但这不是我想要的。 I want to just change $1 with something that gets the job done. 我只想更改可以完成工作的$1

I have also tried: 我也尝试过:

String s = str.replaceAll("regexp", "$1".toUpperCase());

But it looks like "$1".toUpperCase() is "$1" and not the match. 但看起来像是"$1".toUpperCase()"$1"而不是匹配项。 I confirmed it using: 我使用以下方法确认了它:

String s = str.replaceAll("regexp", method("$1"));

// method declared as method()
private static String method(String s) {
    System.out.println(s); // prints "$1"
    return s;
}

Is it even allowed in Java? Java甚至允许吗?

EDIT: 编辑:

String s = "abc";
System.out.println(s.replaceAll("(a)", "$1")); // should print "Abc"

EDIT FOR POSSIBLE DUPE: 编辑可能的重复:

I am not looking for way using m.group() , is it possible using something like \\U$1 in place of $1 with replaceAll() 我不是在寻找使用m.group() ,是否可以使用\\U$1代替replaceAll()来替代$1

\\\\U is not implemented in the java regex AFAIK and you can't do it with a regex as such ( .NET has it IIRC). \\\\U未在Java regex AFAIK中实现,因此无法使用正则表达式来实现( .NET具有IIRC)。 It's a bit verbose, but one way to do it would be: 这有点冗长,但是一种实现方法是:

    String test = "abc";
    Pattern p = Pattern.compile("(a)");
    Matcher m = p.matcher(test);

    StringBuilder sb = new StringBuilder();
    if (m.find()) {
        String match = test.substring(m.start(1), m.end(1));
        m.appendReplacement(sb, match.toUpperCase());
    }

    m.appendTail(sb);
    System.out.println(sb.toString()); 

Since Java 9, we can provide a Function to Matcher#replaceAll(Function<MatchResult,​String> replacer) . 从Java 9开始,我们可以为Matcher#replaceAll(Function<MatchResult,​String> replacer)提供一个Function It is more concise than other answers here. 比这里的其他答案更简洁。 Eg: 例如:

Pattern.compile("regexp")
       .matcher(str)
       .replaceAll(mr -> mr.group().toUpperCase());

We can fully customize this behaviour since we have a hold on MatchResult : 我们可以完全自定义此行为,因为我们拥有MatchResult

Pattern.compile("regexp")
       .matcher(str)
       .replaceAll(mr -> {
                String.format("%s %s", 
                              mr.group(1).toUpperCase),
                              mr.group(2).indent(4);
                   });

I think you should consider StringUtils from Apache Commons. 我认为您应该考虑使用Apache Commons的StringUtils。

This is an example: 这是一个例子:

String s = "abcad";

String replacer = "a";

System.out.println(StringUtils.replaceChars(s, replacer, replacer.toUpperCase()));//<--AbcAd

Pls also consider this avoids you to implement the algorythm that necessarily will be under the hood, and the fact the every jar library introduced in a project is basically a new weak point. Pls还认为这避免了您实施必须在幕后的算法,并且项目中引入的每个jar库基本上都是一个新的弱点。

This is maven dependency: 这是Maven依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM