Jump to content
therks

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

Share this post


Link to post
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)

 


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

Share this post


Link to post
Share on other sites
; mask the pipe with a char combination, i.e. "@#"

$vParamData = 'pipe test=this @# is a pipe|example=value'
$aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3)
For $i = 0 To UBound($aRegEx) -1
    $aRegEx[$i] = StringReplace($aRegEx[$i], '@#', '|') ; now replace with the pipe
Next

_ArrayDisplay($aRegEx)

 

Share this post


Link to post
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

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

Share this post


Link to post
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)

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

Real Programmers aren't afraid to use GOTOs. :P
So I assume that these 1982 scientists would actually use in AutoIt function calls instead of GOTOs

BTW I'm a fake programmer indeed. Does \Q..\E mean literal quiche eater ? :huh2:

Share this post


Link to post
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)

Share this post


Link to post
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

  • Similar Content

    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

       
    • By nend
      This is a program that I made to help my self learn better regular expressions.
      There are a lot of other programs/website with the similar functions.
      But the main advantage of this program is that you don't have to click a button after every changes.
      The program detected changes and react on it.
      Function:
      Match Match of arrays Match and replace Load source data from website Load source data from a website with GET/POST Load text data from file Clear fields Export and Import settings (you can finish the expression a other time, just export/import it) Cheat sheet Generate AutoIt code example code The source code is not difficult and I think most user will understand it.
      In the zip file there is a export files (reg back example), you can drag and drop this files on the gui to import it.
      Download Regex Toolkit Regex toolkit.zip  (Sourcode, example and compiled exe file)
      EDIT: Updated to version V1.2.0
      Changes are:
      Expand and collapse of the cheat sheet (Thanks to Melba23 for the Guiextender UDF) Usefull regular expressions websites links included in the program Text data update time EDIT: Updated to version V1.3.0
      Changes are:
       Automatic generate AutoIt code  Icons on the tab  Few minor bug fixes EDIT: Updated to version V1.4.0
      Changes are:
      Link to AutoIt regex helpfile If the regular expression has a error than the text becomes red Option Offset with Match and array of Matches Option Count with Match and replace Some small minor bug fixed EDIT: Updated to version V1.4.1
      Changes are:
      Small bug in "create AutoIt" code fixed EDIT: Updated to version V1.4.2
      Changes are:
      Small bug in "create AutoIt" code fixed Bug with website data  fixed 
      Regex toolkit.zip  (Sourcode, example and compiled exe file)
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
    • By lee321987
      Hello, I'm trying to match the second to last line of this:
      foo C:\ foobar foobar x C:\temp\dir Last line with chars Here's my code:
      $test = 'foo' & @CRLF $test &= 'C:\' & @CRLF $test &= 'foobar' & @CRLF $test &= 'hello' & @CRLF $test &= 'C:\temp\dir' & @CRLF $test &= 'Last line with chars' & @CRLF $test &= @CRLF $test &= @CRLF $result = StringRegExp($test, '(?m)^C:\\.*$Last.*') MsgBox(0, '', $result) I'm trying to match line "C:\temp\dir".  Anyone have any ideas?
×
×
  • Create New...