iLevi
iLevi

Reputation: 936

Regular expression for extract key value items

I tried to make a regexp to extract key=value items from string. The value string may have or not quotes and spaces (type string or numeric).

The string looks like this:

VAR key = 'value in string mode';
VAR key = 34;
VAR key=3.3;
VAR key='another value without empty spaces between =';

I want extract key and value.

I was develop but is wrong.

VAR \@(\w+)\s?\=\s?\'?([\w]+)\'?\;

Upvotes: 0

Views: 133

Answers (3)

James
James

Reputation: 12806

This will get all data inside of the single quotes using look around behavior.

(?<=').+?(?=')

This will get the key name.

(?<=VAR )[a-zA-Z0-9. ]+(?=\=)

Upvotes: 0

poke
poke

Reputation: 388023

The problem is that \w will only match word characters, so neither spaces nor = signs. Try this (demo):

VAR (\w+)\s?=\s?'?([^']+)'?;

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

That one should do the work:

^VAR ([^ =]+) ?=[ ']*([^']+)'?;$

Upvotes: 1

Related Questions