Raziza O
Raziza O

Reputation: 1646

Key value pair - regex

I have a long text with key value pairs separated by a character '•' (ascii 7) The key must be english letters, digits, and underline - and the value can be everything (also ascii that are not letters)..

The problem I have is that the value can contain '•' as part of its value. Then we need to include it in and and the match should be up to the last '•' of that match.

Manage to do this, but sadly it is not solving my problem.

([A-Za-z0-9_]+)=((.*?)+)•

Test string : MY_KEY_1=○•◘♠ ♥•MY_KEY_2=as dfa sd f@#$•

Expected result: 2 matches MY_KEY_1=○•◘♠ ♥ (NOT MY_KEY_1=○) , MY_KEY_2=as dfa sd f@#$

Upvotes: 1

Views: 59

Answers (1)

revo
revo

Reputation: 48721

It doesn't match up to second since it's matching ungreedily and it doesn't have a good ending point. You could try an ungreedy match up to the point where a key name is following:

(\w+)=(.*?)(?=•\w+=|$)
  • \w => [a-zA-Z0-9_]

  • (?=•\w+=|$) A positive lookahead that looks for either a key name or end of input string

Live demo

Upvotes: 2

Related Questions