maximus
maximus

Reputation: 11524

Regex - Matching a word

I want to create a regex which matches a word in a String:

Miete 920

I want to match the word "Miete".

My regex:

price.matches("=[\bMiete\b]")

However, it doesn`t work? Pls give me a hint.

Upvotes: 0

Views: 86

Answers (2)

Pshemo
Pshemo

Reputation: 124215

If you want to check if some string contains separate word Miete you can use

price.matches(".*\\bMiete\\b.*");

There is no need for = in your regex, also [...] is character class not string literal.

Upvotes: 3

m0skit0
m0skit0

Reputation: 25873

I think your regex is wrong. Try with

price.matches(".*\\bMiete\\b.*")

.* -> 0 or more charcters \\b -> word boundary

So this will match any string that has Miete surrounded by word boundaries.

EDIT: sorry fixed, I forgot how matching works in Java, I'm more used to Perl :)

Upvotes: 2

Related Questions