Tyler Sebastian
Tyler Sebastian

Reputation: 9448

Pattern Matching for Repeated Pattern

I'm trying to extract groups from a string of the format {{field: value} {field2: value} ... {fieldn: valuen}}

I'm stuck at {(.*): (.*)} - in RegexTester, it matches the entire string (understandable), but in execution, it throws an exception - "Illegal repetition"

Upvotes: 1

Views: 119

Answers (2)

RiaD
RiaD

Reputation: 47619

First, { and } should be escaped because they means times to repeat

Second, I think you want to match only parts of your string. You may allow only allowed characters (e.g [a-z0-9]) so that entry string will not be matched

\\{([a-z0-9]*): ([a-z0-9]*)\\}

As pointed in comments you may allow all characters except some special [^{}:]. If I remember correctly you need not escape {} in character class.

Upvotes: 2

morgano
morgano

Reputation: 17422

Escape some chars:

"\\{(.*): (.*)\\}"

characters } and { have a special meaning and need to be escaped.

Upvotes: 0

Related Questions