Jump to content
Sign in to follow this  
SmOke_N

Pattern Matching - Regexp

Recommended Posts

SmOke_N

Ok, time to admit I'm stumped.

I'm trying to come up with an expression that will pull all the capturing groups out of a current expression pattern.

Example, if my pattern string (subject) I wanted to regex was:

"(.*?)(?:\w+)((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))"

I'd want the return results to be:

[0] = (.*?)
[1] = ((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))
[2] = (.+?)
[3] = (.\d.*?(..+\w)(\d\w))
[4] = (..+\w)
[5] = (\d\w)

Any of my fellow RegEx junkies have some ideas?

(And to the smart asses, don't be so naive as to ask for my code wondering if I've attempted it, if I posted what I had, it may actually throw some others off from the right track (which I'm not obviously on)... :D )

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Authenticity

#include <Array.au3>

Dim $sStr = '(.*?)(?:\w+)((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))'
Dim $sPattCap = '(?!\(\?:)(\((?:(?>[^()]+)|(?1))+\))'
;Dim $sPattParentheses = '\((?:(?>[^()]+)|(?R))+\)'
Dim $aResult[1] = [0]
Dim $aRet = StringRegExp($sStr, $sPattCap, 3)

If IsArray($aRet) Then 
    For $i = 0 To UBound($aRet)-1
        $aResult[0] += 1
        ReDim $aResult[$aResult[0]+1]
        $aResult[$aResult[0]] = $aRet[$i]
        _Recursive(StringTrimRight(StringTrimLeft($aRet[$i], 1), 1))
    Next
EndIf

If IsArray($aResult) Then _ArrayDisplay($aResult)
Exit

Func _Recursive($sString)
    Local $aTmp
    
    $aTmp = StringRegExp($sString, $sPattCap, 3)
    If IsArray($aTmp) Then
        For $i = 0 To UBound($aTmp)-1
            $aResult[0] += 1
            ReDim $aResult[$aResult[0]+1]
            $aResult[$aResult[0]] = $aTmp[$i]
            _Recursive(StringTrimRight(StringTrimLeft($aTmp[$i], 1), 1))
        Next
    EndIf
EndFunc

Edit: emoticons..., lol now that I reviewed your post I see you're not a human you're actually a computer with a brain.

Edit2: Change (?>[^()]+) to (?>[^()]*) to match empty parentheses.

Edited by Authenticity

Share this post


Link to post
Share on other sites
Malkey

There's this.

;
   #include <array.au3>
   $str = "(.*?)(?:\w+)((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))"
   #cs
   === Required ======
   [0] = (.*?)  
   [1] = ((?:.*?(.+?))(.\d.*?(..+\w)(\d\w))) 
   [2] = (.+?)  
   [3] = (.\d.*?(..+\w)(\d\w)) 
   [4] = (..+\w)    
   [5] = (\d\w)  
   ============================ 
   #ce
   
   $aArr1 = StringRegExp($str,'\(\.(?:[^()]|\(.[^()]*.\))*.\)',3); [0]|(.*?)[1]|(.+?))[2]|(.\d.*?(..+\w)(\d\w))) ====>0,2,3
   $aArr = StringRegExp($str,'(\({2,6}.*\){2,6})',3) ;[0]|((?:.*?(.+?))(.\d.*?(..+\w)(\d\w))) ====>1
   _ArrayConcatenate($aArr1,$aArr)
   
   $aArr = StringRegExp($str,'\(\.\.[^()]*.\)',3);  [0]|(..+\w)             ====>4
   _ArrayConcatenate($aArr1,$aArr)
   
   $aArr = StringRegExp($str,'\(\\.\\.\)',3)    ;   [0]|(\d\w)              ====>5
   _ArrayConcatenate($aArr1,$aArr)
   
   _ArrayDisplay($aArr1)
  ;
I look forward to seeing your solution.

Share this post


Link to post
Share on other sites
SmOke_N

#include <Array.au3>

Dim $sStr = '(.*?)(?:\w+)((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))'
Dim $sPattCap = '(?!\(\? :) (\((? :( ?>[^()]+)|(?1))+\))'
;Dim $sPattParentheses = '\((? :( ?>[^()]+)|(?R))+\)'
Dim $aResult[1] = [0]
Dim $aRet = StringRegExp($sStr, $sPattCap, 3)

If IsArray($aRet) Then 
    For $i = 0 To UBound($aRet)-1
        $aResult[0] += 1
        ReDim $aResult[$aResult[0]+1]
        $aResult[$aResult[0]] = $aRet[$i]
        _Recursive(StringTrimRight(StringTrimLeft($aRet[$i], 1), 1))
    Next
EndIf

If IsArray($aResult) Then _ArrayDisplay($aResult)
Exit

Func _Recursive($sString)
    Local $aTmp
    
    $aTmp = StringRegExp($sString, $sPattCap, 3)
    If IsArray($aTmp) Then
        For $i = 0 To UBound($aTmp)-1
            $aResult[0] += 1
            ReDim $aResult[$aResult[0]+1]
            $aResult[$aResult[0]] = $aTmp[$i]
            _Recursive(StringTrimRight(StringTrimLeft($aTmp[$i], 1), 1))
        Next
    EndIf
EndFunc

Edit: emoticons..., lol now that I reviewed your post I see you're not a human you're actually a computer with a brain.

Edit2: Change (?>[^()]+) to (?>[^()]*) to match empty parentheses.

This is actually something I had close too.... I'm not sure where the hell I was off at looking at the simple recurse function, but I'll have a look for sure.

** wondering if I even took this approach before, and if I ran into a recursion issue... have you experienced anything like that?

Edit:

Yes, I was running into the double of an expression already used as well...

4 and 5 are basically the same with your example, but I think that can be worked with... would like to hear your feedback

Edit2:

Dammit, I didn't see you made your own experiment... you're going to make me do my homework for sure on (?1).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
SmOke_N

There's this.

;
   #include <array.au3>
   $str = "(.*?)(?:\w+)((?:.*?(.+?))(.\d.*?(..+\w)(\d\w)))"
   #cs
   === Required ======
   [0] = (.*?)  
   [1] = ((?:.*?(.+?))(.\d.*?(..+\w)(\d\w))) 
   [2] = (.+?)  
   [3] = (.\d.*?(..+\w)(\d\w)) 
   [4] = (..+\w)    
   [5] = (\d\w)  
   ============================ 
   #ce
   
   $aArr1 = StringRegExp($str,'\(\.(?:[^()]|\(.[^()]*.\))*.\)',3); [0]|(.*?)[1]|(.+?))[2]|(.\d.*?(..+\w)(\d\w))) ====>0,2,3
   $aArr = StringRegExp($str,'(\({2,6}.*\){2,6})',3);[0]|((?:.*?(.+?))(.\d.*?(..+\w)(\d\w))) ====>1
   _ArrayConcatenate($aArr1,$aArr)
   
   $aArr = StringRegExp($str,'\(\.\.[^()]*.\)',3);  [0]|(..+\w)             ====>4
   _ArrayConcatenate($aArr1,$aArr)
   
   $aArr = StringRegExp($str,'\(\\.\\.\)',3);   [0]|(\d\w)              ====>5
   _ArrayConcatenate($aArr1,$aArr)
   
   _ArrayDisplay($aArr1)
;
I look forward to seeing your solution.
Awesome, thanks Malkey, although I didn't state it (or did I? ), I was trying to stay kind of native with the function. It's something that would have to be easily ported to another language if need be, and _Array funcs unfortunately (although not hard) not that easily/(already implimented) functions.

Edit:

Dammit, that was close... however, order is crucial, if it's not in the right syntactical order, then it doesn't matter if all the values were gathered correctly... It'd throw off everything.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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  

×