Jump to content

Regular expression - capture a character when "escaped", split otherwise


Recommended Posts

I'm looking for a regex genius, cus I'm stumped when it comes to assertions.

So what I have now, is this regular expression: ([^|=]+)=([^|]+)
It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.

Example:

$vParamData = 'example=value|fruit=apple|phrase=Hello world'
$aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3)

; Result
;   [0] => example
;   [1] => value
;   [2] => fruit
;   [3] => apple
;   [4] => phrase
;   [5] => Hello world

So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.

ie:

$vParamData = 'pipe test=this \| is a pipe|example=value'
$aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3)

; I'm getting this:
;   [0] => pipe test
;   [1] => this \
;   [2] => example
;   [3] => value

; But I'd like a result like this:
;   [0] => pipe test
;   [1] => this \| is a pipe
;   [2] => example
;   [3] => value

Is there some pattern that would accomplish this, or am I better off parsing it some other way?

Edited by therks
Link to comment
Share on other sites

there are more efficient ways to do the pieces, but here is replacement method that simply avoids the escaped pipes and then slaps it all back together

#include<array.au3>

$vParamData = 'pipe test=this \| is a pipe|example=value'
$aData = stringsplit(stringreplace(_ArrayToString(stringsplit(stringreplace($vParamData , "\|" , "\*") , "|" , 2) , "=") , "\*" , "\|") , "=" , 2)
_ArrayDisplay($aData)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Might be able to pull off reusing the capture....

$vParamData = 'pipe test=this \| is a pipe|example=value'
msgbox(0, '' , stringreplace(stringregexpreplace($vParamData , "(\\\|)|(=)|(\|)" , "$1" & @LF  ) , "\|" & @LF , "\|"))

 

and without the additional stringreplace, but becoming much more fragile (but works for this specific case).

$vParamData = 'pipe test=this \| is a pipe|example=value'

msgbox(0, '' , stringregexpreplace($vParamData , "(\\\|.*)\||=|(=)|(\|)" , "$1" & @LF ))

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Another skin:

$vParamData = 'pipe test=this \| is a pipe|example=value|another\|example=five|last \|one = one\|two\|three'
$aRegEx = StringRegExp($vParamData, '(.+?)=((?:\\\||[^\\|])+)(?:\||$)', 3)
_ArrayDisplay($aRegEx)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

:evil:

Real programmers use a single function call, quiche eaters use two.
© Ed Post. https://www.ee.ryerson.ca/~elf/hack/realmen.html

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Computed indirect GOTOs, preferably.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...