Sign in to follow this  
Followers 0
GioVit

Need Help with StringRegExp

1 post in this topic

The program listed below, has a function that extract the columns or the rows of a valid given Excel address, I tryed a lot of combinations of patterns in the function StringRegExp, and I think that pattern used in the functions has sense but Doesn't works in some cases.

In the program are comented which Excel address is working fine and which not.

I need some help to solve these little bugs.

Other question is why the pattern to match any char except the ":" "[^:]" sends an error saying invalid pattern?

GetAddr("$AB$1:$AC$100")        ; Result OK
GetAddr("$AB$1:$AC$100",False)  ; Result OK
GetAddr("AB$1:AC$100")          ; Result OK
GetAddr("AB$1:AC$100",False)    ; Result OK
GetAddr("$1:$100")              ; Result OK
GetAddr("$1:$100",False)        ; Result OK
GetAddr("1:100")                ; Result OK
GetAddr("1:100",False)          ; Result OK
GetAddr("$AB:$AC")              ; Result OK
GetAddr("$AB:$AC",False)        ; Result OK
GetAddr("$AB:$AC")              ; Result OK
GetAddr("$AB:$AC",False)        ; Result OK
GetAddr("$C100")                ; Wrong the number (100) returns splitted in two parts from StringRegExp
GetAddr("$C100",False)          ; Result OK
GetAddr("C100")                 ; Wrong the number (100) returns splitted in two parts from StringRegExp
GetAddr("C100",False)           ; Result OK
GetAddr("$100")                 ; Wrong the number (100) returns splitted in two parts from StringRegExp
GetAddr("$100",False)           ; Result OK
GetAddr("100")                  ; Wrong the number (100) returns splitted in two parts from StringRegExp
GetAddr("100",False)            ; Result OK
GetAddr("$C")                   ; Result Ok
GetAddr("$C",False)             ; Wrong says that there is not columns
GetAddr("C")                    ; Result Ok
GetAddr("C",False)              ; Wrong says that there is not columns
                
        
        
Func GetAddr($sRange, $fRows = True)
        $stRange = StringReplace($sRange,"$","")
        if $fRows Then
        ; Get only rows
        ;$aRange = StringRegExp($stRange, "[$]?(?:[:alpha:]{1,2})?[$]?([:digit:]{1,5})([:]?)[$]?(?:[:alpha:]{1,2})?[$]?([:digit:]{1,5})?",1)
        $aRange = StringRegExp($stRange, "([0-9]{1,5})([:]?)(?:[:alpha:]{1,2})?([:digit:]{0,5})?",1)
    Else
        ; Get only Columns
        ;$aRange = StringRegExp($stRange,
([:alpha:]{1,2})(?:[:digit:]{1,5})?([:])?([:alpha:]{1,2})?[$]?(?:[:digit:]{1,5}?)",1)
        $aRange = StringRegExp($stRange, "([:alpha:]{1,2})(?:[0-9]{1,5})?([:]?)([:alpha:]{0,2})?",1)
    EndIf
    $sValor = ""
    if $fRows Then
        $sType = "Rows"
    Else
        $sType = "Columns"
    EndIf
    $Result = @error
    if $Result = 0 Then
        if @extended Then
            For $i = 0 to UBound($aRange) - 1
                $sValor &= $aRange[$i]
            Next
            if UBound($aRange) = 1 Then
                $sValor &= ":" & $sValor
            EndIf

            MsgBox(64,"Result ", $sValor & ", are the " & $sType & " in Range " & $sRange & " isArray = " & IsArray($aRange) & " Array Ubound = " &UBound($aRange,1))
            
        Else
            MsgBox(16, "Result", "There are not " & $sType &  " in Range " & $sRange)
        EndIf
    EndIf
    if $Result = 2 Then
        MsgBox(16, "Error", "Patron Invalido")
        Exit
    EndIf
    Return $sValor
EndFunc

Thanks in Advance

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  
Followers 0