Sign in to follow this  
Followers 0
Trystian

StringRegExpReplace Issue

5 posts in this topic

#1 ·  Posted (edited)

Howdy all,

I can't seem to get this Regular Expression to do what I want in AutoIT, and I am looking for a new set of eyes to see what I'm doing wrong. It works with ASP/VB, but not in AutoIT (v3.1.1.87).

Sample Test Input (not-case sensitive):

$strInput = "Test.01x02.txt"

$strInput = "Test.[01.02].txt"

$strInput = "Test.102.blahblahblah.txt"

$strInput = "Test.0102.blahblahblah.txt"

$strInput = "Test.1x02.blahblahblah.txt"

$strInput = "Test.01x02.blahblahblah.txt"

$strInput = "Test.01.02.blahblahblah.txt"

$strInput = "Test.s1e02.blahblahblah.txt"

$strInput = "Test.s01e02.blahblahblah.txt"

$strInput = "Test.[0102].blahblahblah.txt"

$strInput = "Test.[01x02].blahblahblah.txt"

$strInput = "Test.[01.02].blahblahblah.txt"

$strOutput = StringRegExpReplace($strInput,"(?i)(\S*)\.(?:s|\[)*(\d{1,2}?)(?:x|e|\.)?(\d{2})(?:\])*(?:\S*)\.(txt)","+\1+\2+\3+\4")

Expected $Output= "+Test+1+02+txt" or "+Test+01+02+txt" (depending on input)

Actual $Output = $Input (no change)

Any help would be greatly appreciated.

Thank you,

-Trystian

Edited by TrystianSky

Share this post


Link to post
Share on other sites



Um I know nothing of StringRegExpReplace but from reading the HELP file I came up with this..... It doesn't look that hard to understand.

StringRegExpReplace($strInput, "[.xse]", "+")

Just a thought. I don't believe you can change []'s.

But for a better understanding Please!!

CONSIDER the FOLLOWING!!!! (Bill Nye the Science Guy, BILL BILL BILL!)

StringRegExpReplace

--------------------------------------------------------------------------------

Replace text in a string based on regular expressions.

StringRegExpReplace ( "test", "pattern", "replace", [ count ])

Parameters

test The string to check

pattern The regular expression to compare. See StringRegExp for pattern definition characters.

replace The text to replace the regular expression matching text with. To insert matched group text, \1 - \9 can be used as back-references.

count [optional] The number of times to execute the replacement in the string. The default is 0. Use 0 for global replacement.

Return Value

Check @Error to make sure the function executed properly.

@Error Meaning

0 Executed properly. Check @Extended and the return value to determine if the pattern was found or not.

1 Count invalid. Return value is "".

2 Pattern invalid. Return value is the first location in the string that was invalid, as an integer.

@Extended will be the number of replacements.

Remarks

"test" or "pattern" parameters cannot be a binaryString.

Related

StringReplace, StringRegExp

Example

MsgBox(0, "Regular Expression Replace Test", StringRegExpReplace("Where have all the flowers gone, long time passing?", "[aeiou]", "@"))

Share this post


Link to post
Share on other sites

Yes, I DID read the HELP file prior to posting here. You're right, you don't know anything of StringRegExpReplace, your little example doesn't work, but you really excel at copy/paste though, you should stick with it. B) Thanks anyways. PS: You CAN change the []'s.

If anyone else out there can figure out what I'm attempting to do in my Input/Output examples (pretty simplified), I would greatly appreciate the help. Thank you. :o

-Trystian

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This pattern will match all the examples except the 3rd:

"(?i)(.+)\.[^0-9]*(\d{1, 2})[^0-9]?(\d{2}).*\.(txt)"

In this case, it would probably be easiest to invert the strings so that they can be processed in reverse order. With the current regular expression engine, I think it'll be very difficult to get example 3 to match as well as all the others without searching in reverse order. What reversing the string does is allows matching the "02" which I presume is always going to be a double digit value (After all, I don't know of too many shows that do 100+ episodes a season). Once you get the 2 digits for the episode number, its easy to get the season number.

If you reverse the strings, you should be able to reverse the pattern I show above and it will work.

Edit: Here's the strings being reversed, processed and then re-reversed which will put things exactly how you want it:

#include <String.au3>

Main()

Func Main()
    Local $aStrings[12] = [ _
        "Test.01x02.txt", _
        "Test.[01.02].txt", _
        "Test.102.blahblahblah.txt", _
        "Test.0102.blahblahblah.txt", _
        "Test.1x02.blahblahblah.txt", _
        "Test.01x02.blahblahblah.txt", _
        "Test.01.02.blahblahblah.txt", _
        "Test.s1e02.blahblahblah.txt", _
        "Test.s01e02.blahblahblah.txt", _
        "Test.[0102].blahblahblah.txt", _
        "Test.[01x02].blahblahblah.txt", _
        "Test.[01.02].blahblahblah.txt" _
    ]
;   Local $sPattern = "(?i)(.+)\.[^0-9]*(\d{1, 2})[^0-9]?(\d{2}).*\.(txt)"; Forward
    Local $sPattern = "(?i)(txt)\..*(\d{2})[^0-9]?(\d{1,2})[^0-9]*\.(.+)"; Reverse

    For $i = 0 To UBound($aStrings) - 1
        Local $sResult = StringRegExpReplace(_StringReverse($aStrings[$i]), $sPattern, "\1+\2+\3+\4+")
        ConsoleWrite(_StringReverse($sResult) & @CR)
    Next
EndFunc; Main()
Edited by Valik

Share this post


Link to post
Share on other sites

Thank you very much Valik for taking the time to analyze the problem, and give a working solution. It works perfectly. And the RegEx expression is very elegant.

You are awesome. /bow

-Trystian

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