user590723
user590723

Reputation: 35

Some help with regex in c#

I've been trying to come up with cheeky regexp myself, but no luck so far. I hope one of you can make my day!

Data:

This_is.S0meNam3_.var01.tar
This_is.S0meNam3_.var1.tar.gz
This_is.S0meNam3_.tar.gz

After parsing all the given exmaples I an looking forward to have at leat to groups

Group1 -> Group2  
This_is.S0meNam3_ -> .tar  
This_is.S0meNam3_ -> .tar.gz  
This_is.S0meNam3_ -> .tar.gz

I hope it all makes sense.

Thanks you.

Upvotes: 1

Views: 111

Answers (1)

Try the following:

^(.*?)(?:\.var\d+)?((?:\.tar)?(?:\.gz)?|(?:\.(?:zip|rar)))$

This assumes that the only possible endings are .zip, .rar, .tar, .gz and .tar.gz, and that the things you want removed all are of the form .var<n> for some number <n>.

Getting it to accept any extension isn't really possible without narrowing it down further, as This_is.S0meNam3_.tar.gz could be split into filename and extension in all of the following ways:

This_is                   -    S0meNam3_.tar.gz
This_is.S0meNam3_         -    tar.gz
This_is.S0meNam3_.tar     -    gz

Upvotes: 2

Related Questions