Jump to content
Sign in to follow this  
MouseSpotter

Stumped by StringRegExp that matches delimited strings which contain a substring

Recommended Posts

MouseSpotter

Is there a StringRegExp pattern where you can match strings from within a delimited string (e.g. | delimited) that contain a substring being searched for.

#include <array.au3>
$text="|aaa|kcde|fff|akcde|aak|rpf|k|lir|"
local $allPipedContent = StringRegExp($text,"\|?(.*?)\|",3)
_ArrayDisplay($allPipedContent)

local $allPipedContentThatContainsK = StringRegExp($text,no idea :(,3)
_ArrayDisplay($allPipedContentThatContainsK)
#cs result needs to be
    kcde
    akcde
    aak
    k
#ce

Additionally as the substring is not always k ... it can be any string ...

is there an easy way to convert characters that StringRegExp interprets as control characters into literals that can be searched for (e.g. the "(" in the example below).

|aaa|kcde|fff|(akcde|aak|rpf|k|lir|

Share this post


Link to post
Share on other sites
jdelaney

Well, I'm not able to work out the bugs...but if you do something like this (works for single chars...although the regexp is returning too much/too little relative):

$StringTofind = "\x{" & Hex(Asc("(")) & "}"
$allPipedContent = StringRegExp($text,"\|(.*?" & $StringTofind & ".*?)\|",3)



$StringTofind = "\x{" & Hex(Asc("k")) & "}"
$allPipedContent = StringRegExp($text,"\|(.*?" & $StringTofind & ".*?)\|",3)

got it

#include <array.au3>
$text="|aaa|kcde|fff|akcde|(aak|rpf|k|lir|"
$StringTofind = "\x{" & Hex(Asc("k")) & "}"
$allPipedContent = StringRegExp($text,"([^\|]*" & $StringTofind & "[^\|]*)",3)
_ArrayDisplay($allPipedContent)

$StringTofind = "\x{" & Hex(Asc("(")) & "}"
$allPipedContent = StringRegExp($text,"([^\|]*" & $StringTofind & "[^\|]*)",3)
_ArrayDisplay($allPipedContent)
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
Malkey

Try this. 

#include <array.au3>

Local $sSubString = "k"
Local $text = "|aaa|kcde|fff|akcde|aak|rpf|k|lir|"
Local $allPipedContent = StringRegExp($text, "([^|]*" & $sSubString & "+[^|]*)", 3)
_ArrayDisplay($allPipedContent)

 

....

is there an easy way to convert characters that StringRegExp interprets as control characters into literals that can be searched for (e.g. the "(" in the example below).

|aaa|kcde|fff|(akcde|aak|rpf|k|lir|

"(" converts the "(" character into a literal chatacter.

Also see Help file under the StringRegExp function the "Q" and "E".  Where "Q<From here, all metacharacters are disabled in RegExp pattern to here>E".

 

Edit: Having trouble editing with the square blue AutoIt box. The "Expand" and "Popup" buttons are gone.

Edited by Malkey

Share this post


Link to post
Share on other sites
PhoenixXL

Where "Q<From here, all metacharacters are disabled in RegExp pattern to here>E".

if the string between these contain "E", then it would cause an error. You have to enclose the "" inside a set. like this QSomething[]Esomething moreE.

Otherwise I enclose every non-word/non-space inside space.

Func Escape_Special_SRE( ByRef $String )
    ;special chars for a set.
    $String = StringRegExpReplace( $String, "([\-^\[\]\\])", "\\$1")
    ;Put any non-space non-word char inside a set.
    $String = StringRegExpReplace( $String, "([^\w\s])(\1*)", "[$1$2]")
EndFunc

$String = "\this is a text | this one even..."
Escape_Special_SRE($String)
MsgBox(64, "Escaped String", $String) 

Edit: forgot the special chars of a set, modified the script as pointed out by Malkey

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
MouseSpotter

Thanks for all your solutions

The ones that match what I need are:

 

Try this. 

#include <array.au3>

Local $sSubString = "k"
Local $text = "|aaa|kcde|fff|akcde|aak|rpf|k|lir|"
Local $allPipedContent = StringRegExp($text, "([^|]*" & $sSubString & "+[^|]*)", 3)
_ArrayDisplay($allPipedContent)

"(" converts the "(" character into a literal chatacter.

Also see Help file under the StringRegExp function the "Q" and "E".  Where "Q<From here, all metacharacters are disabled in RegExp pattern to here>E".

 

Edit: Having trouble editing with the square blue AutoIt box. The "Expand" and "Popup" buttons are gone.

 

Plus this to deal with user entering metacharacters 

 

if the string between these contain "E", then it would cause an error. You have to enclose the "" inside a set. like this QSomething[]Esomething moreE.

Otherwise I enclose every non-word/non-space inside space.

Func Escape_Special_SRE( ByRef $String )
    ;Put any non-space non-word char inside a set.
    $String = StringRegExpReplace( $String, "([^\w\s])", "[$1]")
EndFunc

$String = "\this is a text | this one even..."
Escape_Special_SRE($String)
MsgBox(64, "Escaped String", $String)

Share this post


Link to post
Share on other sites
Malkey

^ - [ ] are special characters when found inside a set, [ ... ].

They need to be escaped to lose their special meaning when inside a set.

"QSomething[]Esomething moreE." should be

"QSomething[]Esomething moreE.

Maybe this.

#include <array.au3>

Local $text = "|aaa|kcde|f\Eff|akcde|aak|r\Epf|k|lir|"
Local $sSubString ="\E"
Escape_Special_SRE($sSubString)
;ConsoleWrite($sSubString & @LF)

Local $allPipedContent = StringRegExp($text, "([^|]*" & $sSubString & "+[^|]*)", 3)
_ArrayDisplay($allPipedContent)


Func Escape_Special_SRE( ByRef $String )     ;Put any non-space non-word char inside a set.
    $String = StringRegExpReplace( $String, "([^\w\s])", "[$1]")
    $String = StringRegExpReplace( $String, "(\[)([\^\-\[\]\\])(\])", "$1\\$2$3")
EndFunc
Edited by Malkey

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
Sign in to follow this  

×