Joshwaa
Joshwaa

Reputation: 840

Java Regex Matching Strings

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!

Upvotes: 0

Views: 656

Answers (3)

Andrea Della Corte
Andrea Della Corte

Reputation: 148

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

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35096

You can use Matcher.find() to do this

Upvotes: 0

Brandt Solovij
Brandt Solovij

Reputation: 2134

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

Upvotes: 2

Related Questions