0

Basically I'm trying to match a string inside of a string in Java. For example, I'd want to match "hello" in "hello!there!hello!", matching all of the hellos.

I currently have this but it's not working:

if(word.matches(wordToMatch)) {
word = word.replaceAll(wordToMatch, plugin.greenyPhrases.get(wordToMatch));
}

Any help would be majorly appreciated!

1
  • For better help sooner, post an SSCCE. Commented May 18, 2012 at 16:50

3 Answers 3

2

have you tried

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

edit: heres the full String class notation : http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Sign up to request clarification or add additional context in comments.

1 Comment

It also accepts true regex expressions. :) remember to pay the door man an accept hahaha
0

You can use Matcher.find() to do this

Comments

0

If you want to use the regex engine through the matches method, you need to use .*

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

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

Best

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.