Java正则表达式匹配字符串

Java正则表达式匹配字符串,java,regex,Java,Regex,基本上,我试图在Java中匹配字符串中的一个字符串。例如,我想在“hello!there!hello!”中匹配“hello”,匹配所有hello 我目前有这个,但它不起作用: if(word.matches(wordToMatch)) { word = word.replaceAll(wordToMatch, plugin.greenyPhrases.get(wordToMatch)); } 任何帮助都将不胜感激 你试过了吗 String word = "hello!there!hello!"

基本上,我试图在Java中匹配字符串中的一个字符串。例如,我想在“hello!there!hello!”中匹配“hello”,匹配所有hello

我目前有这个,但它不起作用:

if(word.matches(wordToMatch)) {
word = word.replaceAll(wordToMatch, plugin.greenyPhrases.get(wordToMatch));
}
任何帮助都将不胜感激

你试过了吗

String word = "hello!there!hello!";
word = word.replaceAll("hello", "replaced");

编辑:这是完整的字符串类表示法:

您可以使用Matcher.find()来执行此操作

如果您希望通过matches方法使用正则表达式引擎,则需要使用*

String word = "hello!there!hello!";
String requestedWord = "hello"

if( word.matches( ".*" + requestedWord + ".*" ) )
{
    System.out.println( " This will be printed " );
}

Best

要更快获得更好的帮助,请发布一个。它还接受真正的正则表达式。:)记得付钱给门卫,接受吧哈哈哈