开发者

optional parameter in regular expression

I want to make few paramters optional using regular expression

currently my url is

http://[ipaddress]/StoreData/10/20/30/40/50

开发者_开发问答

as there are 5 numbered parameters , now sometimes i want 4 or sometime 5

i.e. http://[ipaddress]/StoreData/10/20/30/40

should be acceptable

here is my regular expression

(?P<dataone>([0-9])+)/(?P<datatwo>([0-9])+)/(?P<datathree>([0-9])+)/(?P<datafour>([0-9])+)/(?P<datafive>([0-9])+)/


The ? modifier makes a part of a regular expression optional:

(?P<dataone>([0-9])+)/(?P<datatwo>([0-9])+)/(?P<datathree>([0-9])+)/(?P<datafour>([0-9])+)/((?P<datafive>([0-9])+)/)?

Note the (...)? around the "datafive" subpattern.


StoreData/(?P<dataone>([0-9])*)/(?P<datatwo>([0-9])*)/(?P<datathree>([0-9])*)/(?P<datafour>([0-9])*)/?(?P<datafive>([0-9])*)/?$

will be the required regex. Escape the regex, if required.

to Answer Petri Lehtinen and Lasse V. Karlsen, it will handle the trailing backslash also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜