Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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/17.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 如何替换字符串中的所有#{key}?_Java_Regex - Fatal编程技术网

Java 如何替换字符串中的所有#{key}?

Java 如何替换字符串中的所有#{key}?,java,regex,Java,Regex,我有多个#{key}短语的文本。例如: Lorem ipsum dolor sit amet, consectetur adipisicing #{key1}. Proin nibh augue, suscipit a, scelerisque #{key1}, lacinia in, mi. Cras vel #{key2}. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam. Quisque semp

我有多个
#{key}
短语的文本。例如:

Lorem ipsum dolor sit amet, consectetur adipisicing #{key1}. Proin nibh 
augue, suscipit a, scelerisque #{key1}, lacinia in, mi. Cras vel #{key2}. 
Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam. 
Quisque semper #{key3} at risus.
我需要用相应的
messageSource.getMessage(key,null,locale)
messageSource
is)替换所有
{key}
值,但我不擅长正则表达式。如何构建正确的正则表达式

示例

#{texts.appName} need to replace with messageSource.getMessage("texts.appName", null, locale);
#{my.company} need to replace with messageSource.getMessage("my.company", null, locale);

假设
key
只是任何名称的占位符,您的正则表达式如下:
\{([\w\.]+)\}

这意味着:
{
}
之间的任何单词字符或点序列(
\w\.
,相当于
a-zA-Z0-9\.
)将作为组1返回

现在,您需要创建一个匹配器并迭代匹配,提取密钥并用消息替换匹配:

String input = "Lorem ipsum dolor sit amet, consectetur adipisicing #{key1}. " +
   "Proin nibh augue, suscipit a, scelerisque #{key1}," + 
   "lacinia in, mi. Cras vel #{key2}. Etiam pellentesque aliquet tellus." + 
   " Phasellus pharetra nulla ac diam. Quisque semper #{key3} at risus.";
StringBuffer result = new StringBuffer();

Pattern p = Pattern.compile( "#\\{([\\w\\.]+)\\}" );
Matcher m = p.matcher( input );

while( m.find() ) {      
  //extract the message for key = m.group( 1 ) here
  //i'll just mark the found keys 
  m.appendReplacement( result,  "##" + m.group( 1 ) + "##" );      
}
m.appendTail( result );

System.out.println(result); //output: ... consectetur adipisicing ##key1## ...  etc.
试试这个正则表达式:

#{([^}]+)}

使用
yourString.replaceAll(“\\\\{key\\}”,messageSource.getMessage(key,null,locale))


第一个反斜杠是从java的字符串解释中转义第二个反斜杠。第二个是用于转义正则表达式解释的“#”符号(或“{”,“'}”符号)。工作示例:

Pattern p = Pattern.compile("\\Q#{\\E([^.]+)\\Q}\\E");
Matcher m = p.matcher(yourString);
Pattern tempPattern = Pattern.compile("([#{][^.]+[}])");
Matcher tempMatcher = tempPattern.matcher(yourString);


while( m.find() && tempMatcher.find() ) {      

    String textToReplace = messageSource.getMessage(m.group(1), null, locale);
    yourString = yourString.replace(tempMatcher.group(1), textToReplace);
}

System.out.println(yourString);

希望这有帮助

看看api中的String类,它会告诉您所需的一切