tags:

views:

39

answers:

1

The following Regular Expression (url) pattern doesn't match the named group :

^/(.+?)/(.+)?(_p(?<Page>\d+))?

I don't know why this doesn't work in optional block:

?()?

What's the problem with this pattern and how can i get the correct result?

+2  A: 

The 2nd group (.+)? is greedy. Therefore it will match the whole File_p2. Since the 3rd group is optional, it will just be skipped.

You could change the regex to

^/([^/]+)/(.+?)(?:_p(?<Page>\d+))?$

or, slightly more efficiently,

^/([^/]+)/([^_]+(?:_[^_]+)*?)(?:_p(?<Page>\d+))?$
KennyTM
"/Directory/File" as base file and "/Directory/File_p2" etc... so the <Page> groupe needs to be optional
Beni
@Beni: (1) Will file names like `foo_bar_p2` be allowed? (2) Is the Directory a single directory or `/has/a/lot/of/components/`?
KennyTM
There is only one directory and one file in this case (there are other cases, but i need one as template to create the others). foo_bar_p2 is allowed (base file is in this case: foo_bar - e.g. "/Dir_1/File_1" and "/Dir_1/File_1_p2"
Beni