Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式以获取特定字符串_Java_Regex - Fatal编程技术网

Java 正则表达式以获取特定字符串

Java 正则表达式以获取特定字符串,java,regex,Java,Regex,我的字符串是这样的- word1(“word2”)。word3(“word4”)、“{{word5}}”、“Sence6”、“word7”;“word8”、word9)除Sence6外,所有其他均为静态词。我想通过匹配其他单词的值来提取句子6。 我使用正则表达式,但无法处理{{}和()。 那我该怎么办呢? import java.util.regex.Matcher; 导入java.util.regex.Pattern; 最后一个字符串regex=“\\\\\\\”,\“(.*)\”,\“\”;

我的字符串是这样的-
word1(“word2”)。word3(“word4”)、“{{word5}}”、“Sence6”、“word7”;“word8”、word9)
除Sence6外,所有其他均为静态词。我想通过匹配其他单词的值来提取句子6。
我使用正则表达式,但无法处理
{{}
()
。 那我该怎么办呢?

import java.util.regex.Matcher;
导入java.util.regex.Pattern;
最后一个字符串regex=“\\\\\\\”,\“(.*)\”,\“\”;
最后一个字符串String=“word1(\“word2\”)。word3(\“word4\”、\“{word5}}}\”、\“句子6\”、\“\”、\“word7\”、\“word8\”、word9)”;
最终模式=Pattern.compile(regex);
final Matcher Matcher=pattern.Matcher(字符串);
while(matcher.find()){
System.out.println(“完全匹配:+matcher.group(0));

对于(int i=1;i如果要匹配从开始双引号到结束双引号出现的
“句子6”
部分,可以匹配前面的部分,直到开始双引号
}>,“
,然后匹配到下一个双引号,并在组中捕获该部分
([^”]+)

}}\“,\”\Match}}“,” (#捕获组) [^“]+#不匹配”一次或多次 )#近距离捕获组 在java中使用:

\\}\\\\“,\\\”([^\“]+)


您应该先尝试编写自己的代码。尝试先编写代码,发布代码示例,也许我们可以提供帮助。“那么我应该怎么做?”-您应该阅读如何转义正则表达式元字符。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "\\}\\}\", \"(.*)\",\"\"";
final String string = "word1(\"word2\").word3( \"word4\",\"{{word5}}\", \"sentence6\",\"\",\"word7\"|\"word8\", word9 )";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}
}}\", \" # Match }}", " ( # Capturing group [^"]+ # Match not " one or more times ) # Close capturing group