Another RegEx @#$%!

Trying to wrap my head around RegEx’s Look ahead in order to parse a file. Any assistance is greatly appreciated.

String to parse:

Goal:
I would like to take the above line and turn it into key value pairs. Notice the quotes surrounding the values are not consistent and only exist on values containing a space. Example below, I would like to find “PrinterID=” then return the characters contained within the quotes and match the data to that key. If happens the value is not surrounded by quotes, then capture the data up to the following space (\s).

My regex so far:

\\bPrinterID=\\W+(?:\\w+\\W+){1,}\\W

Results:

PrinterID="HP ENVY 110 series" 
  • there is a space to the right of the closing quote

And for any others working on RegEx, this site has helped quite a bit.

Thanks in Advance!

Keys are never quoted, correct?

And how are double-quotes escaped? For example:

"some ""quoted"" text"

or

"some \"quoted\" text"

In the meantime, for just the example you’ve given, this works:

(?x)          # FREE-SPACE MODE

\\b (\\w+) \\b   # key
=
(?|
  " ([^"]*) " # quoted value
  |           # ... or ...
  (\\w+) \\b    # unquoted value
)

[quote=336191:@Kem Tekinay]In the meantime, for just the example you’ve given, this works:

[code]
(?x) # FREE-SPACE MODE

\b (\w+) \b # key

(?|
" ([^"]*) " # quoted value
| # … or …
(\w+) \b # unquoted value
)
[/code][/quote]
You ought to charge $5 for each of these that you do…

You should also always refer to your RegExRX, which is simply a MUST HAVE for each Xojo Dev on macOS. :slight_smile:

Sascha - Thank you for the heads up. I will be picking up a copy shortly. I saw who the author was and immediately did a face palm.

Greg, You had me thinking about sending Kem an Amazon gift card. Which I should do anyway for his help. Sascha’s nudge a bit more effective. - All good, thanks to you both for directing me.

Thank you for the examples. I continue to hear that RegEx is “the way” when parsing. So am heading down the RegEx path. Kem’s app looks like a valuable tool for the toolbox. Kem, any plans for a Windows version? I have to run Windows at work. Will RegExRX do the read ahead option to figure out how that works?

thanks,
Bob

Yes and AFAIK; Yes :smiley:

I bought regexrx long ago, and I still need Kem from time to time for things like this …
although my knowledge in regex greatly improved since !

I only pointed this out because Kem answers everyone’s regex questions (even mine from time to time) without shameless plugs for his software. Kind of a “if I had a fiver for every time someone asked me that, I’d be rich” thing.

All good Greg. I wasn’t aware that Kem had an “app for that”. I picked up a copy this morning. RegEx hands down beats the hell out of instr() and mid() within loops (of course once I get the hang of it).

Even while RegExRX solves most RegEx riddles without a flaw, if it doesn’t it’s a great training/learning Tool.
And it creates native Xojo (and MBS compatible) Code :slight_smile:

As you can clearly see, i love RegExRX :wink:

[quote=336253:@Bob Sellers]Greg, You had me thinking about sending Kem an Amazon gift card. Which I should do anyway for his help. Sascha’s nudge a bit more effective. - All good, thanks to you both for directing me.
[/quote]

I have no idea if you were serious about this, but there is really no need. I’m glad to help.

Send me an email at ktekinay at mactechnologies.com and I’ll send you the Windows registration number. That goes for anyone who bought a MAS version.

Or send $5 via iMessage!