Jump to content
Sign in to follow this  
Izebize

[Solved] StringRegExp help

Recommended Posts

Izebize

$fFile = "X:\Folder\Archive.part1.rar"
ConsoleWrite(StringRegExpReplace($fFile, ".*\\(.*)(?:\.part1)\.rar", "$1")&@CR) ;Works fine
ConsoleWrite(StringRegExpReplace($fFile, ".*\\(.*)(?:\.part1)?\.rar", "$1")&@CR);keeps "part1"

I want to set the ".part1" section may or may not appear. I thought this is the correct syntax, but It doesn't works.

Edited by Izebize

Share this post


Link to post
Share on other sites
Richard Robertson

You used .* which is greedy. It is picking up everything it can. You can instead do a StringInStr match to see if it contains .part1 and remove it with the regex. This would be a simpler option.

Share this post


Link to post
Share on other sites
Izebize

Thanks for your help!

I used StringInStr as you suggested, but now I figured out how to make it work with RegExp:

$fFile = "X:\Folder\Archive.part1.rar"
ConsoleWrite(StringRegExpReplace($fFile, ".*\\(.*)(?:\.part1)\.rar", "$1")   &@CR);Old one
ConsoleWrite(StringRegExpReplace($fFile, ".*\\(.*?)(?:\.part1)?\.rar", "$1") &@CR);Working one
"?" is very important in these cases.

Share this post


Link to post
Share on other sites
Richard Robertson

Whatever works for you. I'm just not used to regex as a replacement. I usually work on matches and groups not replacements.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.