beginner_
beginner_

Reputation: 7622

Match anything before a certain pattern

I have string that can contain almost any character including (_- % and so forth. The string ends with (\d{1,2}). Eg. parenthesis with 1 or 2 digits. I now want 2 capturing groups, the 2 digits and everything before the parenthesis.

currently I have:

final Pattern pattern = Pattern.compile("^([-%\\(\\)_/= a-zA-Z\\d]+)\\((\\d{1,2})\\)$");

But this does not match everything. I want to replace the char group with .* but not have it match the (\d{1,2}) at end of string. How can I achieve this?

Upvotes: 3

Views: 132

Answers (1)

Mena
Mena

Reputation: 48404

If I understand your question, you can use a reluctant quantifier .*? instead of your greedy quantifier .* to match everything reluctantly before your second catpure group.

Upvotes: 6

Related Questions