Jump to content
Sign in to follow this  
Insolence

RegExp goop

Recommended Posts

More problems:

$Input = 'SetTitle "String", 1, 2,3, 5,  "Should return all of these"   ,"Literal use of quotes: \"\"" ' 

; Description of the RegExp I am using:
;    Find the function name
;
;        Find a numeric parameter
;    OR 
;        A string parameter
;           Start at the first ", move to the next " that isn't preceded by a \
$arParameter = StringRegExp ( $Input,    '(\w*) '        & _     
                                        '(([0-9]+)'  & _     
                                        '|'          & _     
                                        '(".*?[^\\]"))'   _  
                                        , 3)

For $i = 0 to UBound($arParameter) - 1
    MsgBox("","", $arParameter[$i] )
Next

I'm horrible at explaining things... running this should show the problems :lmao:

PS

I want to get rid of the start/end quotes... whenever I try to do that while keeping the quotes preceded by \ it doesn't work correctly.


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Well, it looks like all of your commands are comma seperated, no?

Why not just use StringSplit, and then work with those results? I think you might be overcomplicating things.

Err.. oops. I guess I forgot something, getting comma's out of strings will be tough...

See, I'm stuck in the code trying to write something that would parse this:

'SetTitle "String", 32, 48, "She said, \"Hello.\""'

You would want array with the following, right?

0: SetTitle "String"

1: 32

2: 48

3: She said, "Hello."

Edited by Saunders

Share this post


Link to post
Share on other sites

I already did it without RegExp. It's a little buggy but it works, I'd much rather use RegExp.

;==============================================================================
; Returns
;==============================================================================
; Returns an array of the parsed parameters.
;
; Errors:
;  1 - Uneven number of quotes
;  2 - Extra comma outside of quotes

;==============================================================================
; Test String
;==============================================================================
$Input = 'SetTitle "String", "This is a string, WITH a comma, or two, !", 5, 5, 5, "Should return all of these", "Literal use of quotes: \"\"" ' 

;==============================================================================
; Goods
;==============================================================================
; Remove function, we don't need it
$FirstSpace = StringInStr ( $Input, " " )
$Parameters = StringMid ( $Input, $FirstSpace )

; Setup param count
$ParamCount = 0

; Checking all possible comma's for a new parameter
$ParamSplit = StringSplit($Parameters, ',')

;For $i = 1 to $ParamSplit[0]
;   MsgBox("","", $ParamSplit[0] & "   -" & $ParamSplit[$i] & "-" )
;Next

; Setting up parameter array to max possible parameters (each comma is a possible new parameter)
Dim $Parameter[$ParamSplit[0]]

For $i = 1 to $ParamSplit[0]
    IsInQuotes ( $ParamSplit[$i] )
    
; If not in quotes, add a new parameter
    If @error = 0 Then 
   ; If no value and outside of quotes, must be an extra comma
        If $ParamSplit[$i] = "" Then
            SetError(2)
            Return -1
        EndIf
        
   ; Subracting one from the array to compensate for the StringSplit array
   ; and removing quotes.  Also making sure '\"' are parsed correctly
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '\"', "{QUOTE}" )
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '"', "" )
        $ParamSplit[$i] = StringReplace( $ParamSplit[$i], '{QUOTE}', '"' )
        
        $Parameter[$ParamCount] = _StringTrimWS( $ParamSplit[$i] )
        
   ; Check 1 to the param count
        $ParamCount = $ParamCount + 1
    ElseIf @error = 1 Then  
   ; If comma IS found inside quotes, combine it with the next split
        $ParamSplit[$i+1] = $ParamSplit[$i] & "," &$ParamSplit[$i+1]
    EndIf
Next

; Getting rid of any extra parameter stuff
ReDim $Parameter[$ParamCount]
    
For $i = 0 to UBound($Parameter) - 1
    MsgBox("","", $Parameter[$i])
Next

;==============================================================================
; Functions
;==============================================================================
Func StringCount ( $String, $Delimeter )
    Local $Count
    
    $Count = StringSplit ( $String, $Delimeter )
    
    Return $Count[0] - 1
EndFunc

Func _StringTrimWS ( $String )
    Return StringStripWS ( StringStripWS ( $String, 1 ), 2 )
EndFunc

Func CombineStrings( $arString )
    Local $ReturnString
    
    If IsArray($arString) = 0 Then Return -1
        
; Combine all parameters
    For $i = 0 to UBound($arString) - 1
        $ReturnString = $ReturnString & $arString[$i] & " "
    Next
    
; Get rid of last " "
    $ReturnString = StringTrimRight ( $ReturnString, 1 )
    
    Return $ReturnString
EndFunc

Func IsInQuotes ( $String, $Pos = -1)
    Local $Check, $occurances, $increment
    
; Shrink $String from beginning to the character
    $String = StringMid ( $String, 1, $Pos )
    
    $increment = 1
    $occurances = 1 
    Do
        $Check = StringInStr ( $String, '"', -1, $increment )
        
   ; Check for a \ before a quote, to signal it's not the escaping quote
        If StringMid( $String, $Check - 1, $Check) <> '\"' Then $occurances = $occurances + 1
            
        $increment = $increment + 1
    Until $Check = 0
    
; Occurances of quotes is even, so it must be outside quotes
    If IsInt($occurances / 2) Then 
        Return 1
    Else    
        SetError(1)
    EndIf
EndFunc

But, if you change the string to use an odd number of \"'s it seems to mess up:

;==============================================================================
; Test String
;==============================================================================
$Input = 'SetTitle "String", "This is a string, WITH a comma, or two, !", 5, 5, 5, "Should return all of these", "Literal use of quotes: \"\"\"" '

I want an array like this:

[0] = Function Name

[n] = Parameter value

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

;==============================================================================
; Test String
;==============================================================================
$Input = 'SetTitle "String", "This is a string, WITH a comma, or two, !", 5, 5, 5, "Should return all of these", "Literal use of quotes: \"\"\"" '

I want an array like this:

[0] = Function Name

[n] = Parameter value

<{POST_SNAPBACK}>

So using your last example there, you want this?

0: SetTitle

1: String

2: This is a string, WITH a comma, or two, !

3: 5

4: 5

5: 5

6: Should return all of these

7: Literal use of quotes: """

Is that right? (Just want to be clear).

By the way, as complex as this is getting, I'm damn near sure there's no way to do it using solely regex.

*Edit: Now this is weird... StringRegExp('.*', 'Hello') is returning 0. Am I getting daft, or is this just plain bugged?

*Edit2: Nevermind, I'm an idiot... getting my parameters backwards.

Edited by Saunders

Share this post


Link to post
Share on other sites

Yes that's how I want it returned but... you bastard!

Regexp was going to be my 1-liner :lmao:

Anyway, if it can't handle it, than so be it. If you can get it to work better than mine, I appriciate it. Also, thanks for throwing me the help. I see you like doing this string-parsing stuff as much as I.

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

As to your edit, I don't know :lmao:

I'm using the latest beta in the dev section, let me test that..

MsgBox("","", StringRegExp('\w+', 'Hello',0))

I tried all sorts of things, each returned 0.

Agh, you just wasted my time o:)

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

I did get this... see how well it works for you. It might not be a one liner, but it's shorter than your other code. :lmao:

$Input = 'SetTitle "String", "This is a string, WITH a comma, or two, !", 5, 5, 5, "Should return all of these", "Literal use of quotes: \"\"\"" '

$Params = ReadParams($Input)

For $i = 0 to UBound($Params) - 1
    MsgBox("","", $Params[$i])
Next

Func ReadParams($s_Input)
    $s_Input = StringReplace($s_Input, '\"', Chr(1))

    $a_Params = StringRegExp($s_Input, '([0-9]+)|"(.*?)"|(\w+)', 3)

    For $i = 0 to UBound($a_Params) - 1
        $s_Param = $a_Params[$i]
        $s_Param = StringReplace($s_Param, Chr(1), '"')
        $a_Params[$i] = $s_Param
    Next
    
    Return $a_Params
EndFunc

Share this post


Link to post
Share on other sites

Ah, perfect.

Owe you a few now :lmao:


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...