Click here to Skip to main content
15,073,679 members
Articles / Desktop Programming / QT
Posted 31 Mar 2014


8 bookmarked

Parsing a Key-Value pair with a Regular Expression

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
6 Sep 2014CPOL2 min read
A walk-through on how a Key-Value pair can be parsed using a PERL-compatible regex engine
This Tip demonstrates a solution using Qt. The regular expression itself can be used with any PERL-compatible regex engine. You can find the discussion which lead me to write this Tip at the RegEx Forum.


Parsing a Key-Value pair ain't that hard, you say? I'm sure it isn't if you keep it as simple as possible and define that a Key can't contain anything else than characters and numbers, a value can't contain anything else than characters and numbers and they are separated by a '=':

Key1=Value1 Key2=Value2

But what if we advance that a bit? I want a Value to contain white-spaces, optional ones:

Key1=Value 1 Key2=Value Key3=Value 3

We get somewhere, as you can see. But I want more. I want the value to possibly contain everything. Of course this leads us to a problem, because the '=' is already reserved as separator between Key and Value - It's solvable by escaping the '=' if it occurs in a value. I made up this practical example where this pattern might be of use:

ErrorMessage=The file was not found. Path\=C:/Temp/File.txt ErrorNumber=12312

Using the regular expression

The solution presented here is feasible in any PERL-compatible regex engine, even though I will use Qt to demonstrate it's use. The regex looks rather distracting if you look at it for the first time:


The regex looks strange at first, but as soon as you put it into Expresso you can see what it means more clear:

Image 1

The regex essentially contains two different capture groups, one being the key ((\b[^\s=]+)) and the other one being the value ((([^=]|\\=)+)). These two captures must be separated by a '='. A key can contain anything but a white-space or a '=' and a value can contain anything but an unescaped '='. Each sequence can occur with any number of repetitions.

Now that you know what the regex essentially does, you also need to be able to parse a string using the previously described regex. Something important also remains to be said: The proposed regex does only return the last Key-Value pair, therefore we need to process the input string multiple times.

//Regular expression as descripted at 
QRegularExpression regex("^((\\b[^\\s=]+)=(([^=]|\\\\=)+))*$");
QString example = "ErrorMessage=File wasn't found_Path\=C:/Temp/File.txt ErrorNumber=12312";
while(example.length() > 0){//As long there is stuff in example
   //Get the last Key-Value pair from the RegEx
   QRegularExpressionMatch keyValueRegexMatch = keyValueRegex.match(keyValueRawData); 

   //Output the found Key-Value Pair

   //Remove the replaced Key-Value pair from the input
   example = example.replace(keyValueRegexMatch.captured(1), "");

The above solution isn't far from perfect, yet it needs some tweaking: If a captured group is not at the end of a string, it happens that the space between Value and the following key isn't removed.

Points of interest

It's fascinating how powerful regular expressions are. But this example has also showed me that not all regex engines are working the same way, and sometimes you need to tweak a regex to get it work on a specific engine, even though it has perfectly worked with another engine. I tested this regex with the Qt regex engine (QRegularExpression, to be exact - See here for a distinction to QRegExp) and the .Net regex engine, yet I'm confident that it will work well with most of the popular regex engines out there.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marco Bertschi
Software Developer
Switzerland Switzerland
I'm a young Software Engineer. Whilst I may accept a 'status quo' as given, it's my responsibility to improve it as soon as the opportunity arises.
Very familiar with the .Net framework since it has been the underlying foundation for most of my professional work so far. I'm that kind of guy who gets stuff done, while I may also improve the way things work whilst I'm at it. Generally can get along well with most people I meet as a professional.
Fervent volunteer firefigter, captain-ranked air force militia spec officer.
Great friend and generally nice to have me around - For a talk, a beer, or a bonfire (I might as well do all of those things combined). Oh, and I really love music - Rock & country that is, mostly.

Comments and Discussions

SuggestionLink to source Pin
Richard Deeming10-Apr-14 9:16
mveRichard Deeming10-Apr-14 9:16 
GeneralRe: Link to source Pin
Marco Bertschi10-Apr-14 9:37
professionalMarco Bertschi10-Apr-14 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.