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.
精彩评论