tags:

views:

90

answers:

1

I have an input like this test1.test2.part01 which I want to strip away to test1.test2. The only thing i know is that it will end with partxx and probably a dot before the partxx. However, it will not always be a apart. Another example of input might be testas1.tlp2.asd3.part10 which ofcourse should be stripped to testas1.tlp2.asd3.

I've made all that, no problem. The problem is the dot at the end before partxx. My regex at the moment is:

(.*)\.?part\d{1,2}

But it will include the dot in the group. I do not want the dot to be in the group. The below works as I want it, given that the dot exists, but it will not always be there.

(.*)\.part\d{1,2}

How can I exclude the optional dot from the group?

+3  A: 

Escape the dot

(.*)\.?part\d{1,2}

The way you have it, the dot is interpreted as meaning "any character" rather than a literal dot.

Alternatively,

s/\.part\d\d?$//;
Ed Guiness
Thanks, can't believe I missed that. Even if I escape it it will be in the group
Oskar Kjellin
I suspect that this will still include the dot in the group since the quantifier inside the group is greedy. `(.*?)\.?part\d{1,2}` might work better.
Tim Pietzcker
@Tim That seemed to do the trick! :) Thanks
Oskar Kjellin