One Two Three
One Two Three

Reputation: 23537

pattern for matching a string

I'm trying to create a java.util.regex.Pattern that would match strings similar to the following:

Unexpected ID 'foo_<some_number>': ERR-125:"IDs": invalid id

So I thought the pattern should just be like this:

Pattern.compile("Unexpected ID 'foo_*': ERR-125:\"IDs\": invalid id");

But that didn't work. Could anyone tell me why it didn't work? What should I have done instead?

Thanks

Upvotes: 0

Views: 48

Answers (2)

AndreaTaroni86
AndreaTaroni86

Reputation: 559

Asterisk use is wrong, try with \d.

Sorry, you have already done it... well.

Upvotes: 0

Mena
Mena

Reputation: 48444

The issue in your Pattern is that you are using a 0 or more (greedy) quantifier on the underscore.

Try this:

String input = "Unexpected ID 'foo_1': ERR-125:\"IDs\": invalid id";
//                                                   | any digit
//                                                   |  | once or more (greedy)
System.out.println(input.matches("Unexpected ID 'foo_\\d+': ERR-125:\"IDs\": invalid id"));

Output

true

Upvotes: 4

Related Questions