ata
ata

Reputation: 9011

Match all characters except one

I have a test string with pattern

{something1=any_character} any_character {something2=any_character}

I wanted to match the first bracketed sequence and in next iteration I wanted to match the next. So I am using Pattern and Matcher.

The regex I have is \{(\w)*something1.*(?!=).*(?!\}) but the problem is that it match the complete string rather then just the first one. I want to break the first search at first } and then find next }.

Upvotes: 0

Views: 83

Answers (2)

Toto
Toto

Reputation: 91385

Just make the wildcard lazy:

\\{(\\w)*?something1.*?(?!=).*?\\}

Upvotes: 1

TheLostMind
TheLostMind

Reputation: 36304

If you are using Pattern and Matcher, then only capture groups like this :

(\\{\\w+=\\w+\\}) and use a while() loop with matcher.find()

Upvotes: 1

Related Questions