Jump to content

StringRegExp()


Recommended Posts

Okay I have been trying to write some Regular Exp​ressions to be sure I can pull out particular sections of a string that change on occasion.

TimeCodeDisplay_HH_MM_SS_FF

TimeCodeDisplay_MM_SS_FF

TimeCodeDisplay_MM_SS

Inside "The Regex Coach" the following Regular Exp​ression worked.

((?-i)[HMSF]{2,2}_?){2,4}

I tried it in my AutoIt script as follows:

This would return MM_SS_FF or MM_SS, it would return blank when the HH_MM_SS_FF was selected.

If anyone has some insight as to what I am doing wrong, I would greatly appreciate it. I don't know if I need to use a different Regular Exp​ression checker, but I have checked on http://www.regular-exp​ressions.info/ to be sure my exp​ressions are valid for PCRE, and they are.

Thanks in Advance,

Jarvis

Edited by Jon

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

?

Dim $Arr[3] = ['TimeCodeDisplay_HH_MM_SS_FF', 'TimeCodeDisplay_MM_SS_FF', 'TimeCodeDisplay_MM_SS']
For $i = 0 To 2
    $sRet = StringRegExpReplace($Arr[$i], "(?U-i:.*)(_[HMSF]{2})", "\1")
    MsgBox(0, "Test", StringTrimLeft($sRet, 1))
Next
Link to comment
Share on other sites

Hum I was looking at some of the online RegEx checkers and they gave different results sometimes-

http://www.regextester.com/

http://www.spaweditor.com/scripts/regex/

http://regexlib.com/RETester.aspx

http://www.regextester.com/

The stuff you did seems correct I think ( maybe just (HH_MM_SS_FF|MM_SS_FF|MM_SS) would work if they are the only possibilities)...

Well there's always StringReplace($a_Value[1],"TimeCodeDisplay_","") :)

Edited by evilertoaster
Link to comment
Share on other sites

Hey,

heres a couple of ways to do it...

#include <Array.au3>

;~ $sRegEx = "\A[^_]+_(.+)" ; as simple as this
;~ $sRegEx = "_([HMSF]{2})" ; simply get changing letters as an array
;~ $sRegEx = "[HMSF]{2}\z" ; simply get the last two letters
;~ $sRegEx = "((?:_[HMSF]{2}){2,4})\z" ; get the whole changing part of the string (or if you just want the last to change {2,4} to {2})
$sRegEx = "_((?:[HMSF]{2}_?){2,4})\z" ; get the whole changing part of the string without leading underscore

Dim $Arr[3] = ['TimeCodeDisplay_HH_MM_SS_FF', 'TimeCodeDisplay_MM_SS_FF', 'TimeCodeDisplay_MM_SS']
For $i = 0 To 2
    $sRet = StringRegExp($Arr[$i], $sRegEx, 3)
    If IsArray($sRet) Then _ArrayDisplay($sRet)
Next

Edit: added regex

Edited by Robjong
Link to comment
Share on other sites

@Everyone

Thanks for the many replies. I find it unfortunate that none of the proposed solutions work except for (HH_MM_SS_FF|MM_SS_FF|MM_SS). That's the only one that worked. The rest wouldn't return an array. I guess I will stick with that solution for now though the point of the RegExp was incase they wanted to add something along the lines of HH_MM_SS, HH_MM to the available formats.

Thanks again,

Jarvis

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

...I find it unfortunate that none of the proposed solutions work except for (HH_MM_SS_FF|MM_SS_FF|MM_SS)...

strange, this one works just as well for me

"_((?:[HMSF]{2}_?){2,4})"

test script

#include <Array.au3>


Dim $aTimeCodes[5] = ['TimeCodeDisplay_HH_MM_SS_FF', 'TimeCodeDisplay_MM_SS_FF', 'TimeCodeDisplay_MM_SS', 'TimeCodeDisplay_HH_MM_SS', 'TimeCodeDisplay_HH_MM']
Dim $aResults[6][3] = [["Default", "(HH_MM_SS_FF MM_SS_FF MM_SS)", "_((?:[HMSF]{2}_?){2,4})"],["HH_MM_SS_FF", "", ""],["MM_SS_FF", "", ""],["MM_SS", "", ""],["HH_MM_SS", "", ""],["HH_MM", "", ""]]


For $i = 0 To 4
    $aResults[$i + 1][1] = ""
    $mRes = StringRegExp($aTimeCodes[$i], "(HH_MM_SS_FF|MM_SS_FF|MM_SS)", 1)
    If IsArray($mRes) Then $aResults[$i + 1][1] = $mRes[0]
Next

For $i = 0 To 4
    $aResults[$i + 1][2] = ""
    $mRes = StringRegExp($aTimeCodes[$i], "_((?:[HMSF]{2}_?){2,4})", 1)
    If IsArray($mRes) Then $aResults[$i + 1][2] = $mRes[0]
Next

_PrintResults($aResults)
_ArrayDisplay($aResults)


Func _PrintResults($aArray, $sTitle = "")
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    
    ConsoleWrite("-> " & $aArray[0][0] & @CRLF)
    For $i = 1 To UBound($aArray) - 1
        ConsoleWrite($aArray[$i][0] & " ")
    Next
    ConsoleWrite(@CRLF & @CRLF)
    
    ConsoleWrite("-> " & $aArray[0][1] & @CRLF)
    For $i = 1 To UBound($aArray) - 1
        ConsoleWrite($aArray[$i][1] & " ")
    Next
    ConsoleWrite(@CRLF & @CRLF)
    
    ConsoleWrite("-> " & $aArray[0][2] & @CRLF)
    For $i = 1 To UBound($aArray) - 1
        ConsoleWrite($aArray[$i][2] & " ")
    Next
    ConsoleWrite(@CRLF)
EndFunc

_PrintResults

-> Default
HH_MM_SS_FF MM_SS_FF MM_SS HH_MM_SS HH_MM 

-> (HH_MM_SS_FF MM_SS_FF MM_SS)
HH_MM_SS_FF MM_SS_FF MM_SS MM_SS  

-> _((?:[HMSF]{2}_?){2,4})
HH_MM_SS_FF MM_SS_FF MM_SS HH_MM_SS HH_MM

so it seems to return the correct values and would also work on the values you might add, or am i missing something?

Edit: code tag

Edited by Robjong
Link to comment
Share on other sites

@Everyone

Thanks for the many replies. I find it unfortunate that none of the proposed solutions work except for (HH_MM_SS_FF|MM_SS_FF|MM_SS). That's the only one that worked. The rest wouldn't return an array. I guess I will stick with that solution for now though the point of the RegExp was incase they wanted to add something along the lines of HH_MM_SS, HH_MM to the available formats.

Thanks again,

Jarvis

I don't remember seeing where you asked that the results be an Array :)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...