[英]Trying to find proper Regular expression
I am trying to make a regular expression which matches with both of the following urls: 我正在尝试制作一个与以下两个URL匹配的正则表达式:
http://honey.myPage.com/national/
http://honey.myPage.com/national
i mean url with ending slash and without ending slash i am able to make following pattern which is currently only recognizing pattern with slash only. 我的意思是带有结尾斜杠而没有结尾斜杠的url我能够做出以下模式,该模式当前仅识别带有斜杠的模式。 can anyone suggest me either a different pattern which can recognize both kind of urls
谁能建议我一个可以识别两种网址的不同模式
/\.myPage\.com\/(\D+[\/])+(index.htm[l]?)?/
Any Help will be appericiated 任何帮助都将适用
Could you clarify your intent with \\D ? 您能否用\\ D阐明您的意图? it matches any non digit , so would match also /, ?, !, ... I don't think this was your intent?
它匹配任何非数字 ,所以也匹配/,?,!,...我不认为这是您的意图?
Assuming you want to match: 假设您要匹配:
.myPage.com/pretty/mutch/any/uri/provided
.myPage.com/pretty/mutch/any/uri/provided/
.myPage.com/pretty/mutch/any/uri/provided/index.htm
.myPage.com/pretty/mutch/any/uri/provided/index.html
I'd go with something like this: 我会选择这样的东西:
\.myPage\.com(\/\w+)+?(\/(index\.html?)?)?
(\\/\\w+)+?
- matches /sometext
, repeated one or multiple times, but as little as possible to work (necessary to handle the rest of the rule properly) -匹配
/sometext
,重复一次或多次,但应尽可能少地工作(必须正确处理其余规则)
(\\/(index\\.html?)?)?
should be decomposed: 应该分解:
( ( /(index.html?)?
/(index.html?)? )?
)? - the whole block is optional
-整个块是可选的
/(index.html?)? /(index.html?)? - would match
/
or /index.htm
or /index.html
-与
/
或/index.htm
或/index.html
匹配
Hope this helps. 希望这可以帮助。
A questionmark stands for 0 or 1. So you have to add a questionmark after the slash. 问号代表0或1。因此,您必须在斜杠后添加一个问号。 Change it to this:
更改为此:
/\.myPage\.com\/(\D+\/?)+(index.htm[l]?)?/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.