提取 Java 中的正则表达式内部值

huangapple go评论55阅读模式
英文:

Extract regex internal values in Java

问题

可以使用Java函数来实现这个任务,无需编写额外的例程。以下是一个示例代码,可以帮助您实现所需的文本替换:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String inputText = "$F{abc} and $F{def}";
        
        // 使用正则表达式匹配 $F{...} 并提取其中的内容
        Pattern pattern = Pattern.compile("\$F\\{(\\w*)\\}");
        Matcher matcher = pattern.matcher(inputText);
        
        // 替换匹配到的文本
        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            String replacement = matcher.group(1); // 获取匹配到的内容
            matcher.appendReplacement(result, replacement);
        }
        matcher.appendTail(result);
        
        String outputText = result.toString();
        System.out.println(outputText);
    }
}

运行此代码后,您将获得所需的输出:

abc and def

这段代码使用正则表达式来匹配 $F{...} 格式的文本,并提取其中的内容,然后用提取的内容替换原始文本中的匹配项。

英文:

Given this text:

$F{abc} and $F{def}

I need to get

abc and def

For that, I would use this regex to find the values \$F\{\w*\} but I need to get what's represented by w*:

str.replaceAll("\$F\{\\w*\\}", "??" );

Is this doable with a Java function or I need to write the routine?

答案1

得分: 0

str = str.replaceAll(""\\\$F\\\\{(\\\\w*)}"", "$1");
英文:

You can capture the text in a group:

str = str.replaceAll("\\$F\\{(\\w*)}", "$1");

答案2

得分: 0

更新

我没有完全阅读您的问题。感谢The fourth bird 指出这一点。以下是期望输出 abc and def 的代码

public class Main {
    public static void main(String[] args) {
        String str = "$F{abc} and $F{def}";
        System.out.println(str.replaceAll("\$F\\{(\\w*)\\}", "$1"));
    }
}

输出:

abc and def

您只需要用捕获组(1)替换给定的字符串。原始答案中的解释对此更新仍然有效。

原始答案:

您可以使用正则表达式 (\$F\{)(\w*)(\}),将捕获组(2)替换为 ??,同时保留捕获组(1)和捕获组(3),如下所示:

public class Main {
    public static void main(String[] args) {
        String str = "$F{abc} and $F{def}";
        System.out.println(str.replaceAll("(\$F\\{)(\\w*)(\\})", "$1??$3"));
    }
}

输出:

$F{??} and $F{??}

查看这里以获取正则表达式 (\$F\{)(\w*)(\}) 中所有捕获组的示例。

英文:

Update

I didn't read your question completely. Thanks to The fourth bird for pointing it out. Given below is the code for the expected output, abc and def

public class Main {
	public static void main(String[] args) {
		String str = "$F{abc} and $F{def}";
		System.out.println(str.replaceAll("\$F\\{(\\w*)\\}", "$1"));
	}
}

Output:

abc and def

All you need to do is to replace the given string with the capturing group(1). The explanation in the original answer is still valid for this update.

Original answer:

You can use the regex, (\$F\{)(\w*)(\}) and replace the capturing group(2) with ?? and preserve the capturing group(1) and the capturing group(3) as shown below:

public class Main {
	public static void main(String[] args) {
		String str = "$F{abc} and $F{def}";
		System.out.println(str.replaceAll("(\$F\\{)(\\w*)(\\})", "$1??$3"));
	}
}

Output:

$F{??} and $F{??}

Check this for an illustration of all the capturing groups in the regex, (\$F\{)(\w*)(\}).

答案3

得分: -1

One way is to use the regex (\\$F\\{)|(\\}) you can both remove the $F{ and } parts using replaceAll():

String str = "$F{abc} and $F{def}";
str = str.replaceAll("(\$F\\{|\\})", "");

About regex:

  • (\\$F\\{) : 1. group $F{
  • | : OR
  • (\\}) : 2. group }

The other way is to use a capture group reference in the replacement parameter. $1 stands for the capture group (\\w*) that corresponds to abc and def:

String str = "$F{abc} and $F{def}";
str = str.replaceAll("\$F\\{(\\w*)\\}", "$1");
System.out.println(str);

Output:

abc and def
英文:

One way is to use the regex (\\$F\\{)|(\\}) you can both remove the "$F{" and "}" parts using replaceAll():

String str = "$F{abc} and $F{def}";
str = str.replaceAll("(\$F\\{)|(\\})", "");

About regex:

  • (\\$F\\{) : 1. group "$F{"
  • | : OR
  • (\\}) : 2. group "}"

Other way is to use a capture group reference in the replacement parameter. $1 stands for the capture group (\\w*) that corresponds to abc and def

String str = "$F{abc} and $F{def}";
str = str.replaceAll("\$F\\{(\\w*)\\}", "$1" );
System.out.println(str);

Output:

abc and def

huangapple
  • 本文由 发表于 2020年8月11日 23:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63361826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定