I need a regular expression which matches:
我需要一个匹配的正则表达式:
http://example.com/foo
http://example.com/foo/
http://example.com/foo/bar
but not:
http://example.com/foobar
Using http://example.com/foo/?
matches the three types, but it matches /foobar
too that I don't want. What should I add to the regex to not match /foobar
?
使用http://example.com/foo/?匹配这三种类型,但它匹配/ foobar也是我不想要的。我应该在正则表达式中添加什么来匹配/ foobar?
5 个解决方案
#1
4
Try this one:
试试这个:
^http://example\.com/foo(?:/.*)?$
#2
1
In your regex, the last /?
means an optional /
at the end. So /foobar
is also matched. Try this:
你的正则表达式,最后/?意味着可选/最后。所以/ foobar也匹配。试试这个:
http:\/\/example\.com\/foo($|\/.*)
#3
0
Try something like this:
尝试这样的事情:
http://example.com/foo(?:\/|/(\w+)|)
In regex form:
以正则表达式:
/http:\/\/example.com\/foo(?:\/|\/(\w+)|)/
This will match example.com/foo or example.com/foo/bar or example.com/foo/
这将匹配example.com/foo或example.com/foo/bar或example.com/foo/
Some explaination:
-
(foo|bar)
matches foo or bar -
(?:)
a group with the?:
in the begin will not been captured -
\/
will match a / at the end -
\/(\w+)
match a / with a word character who is repeated one or more times -
|)
will match nothing at the end of the string.
(foo | bar)匹配foo或bar
(?:)一个组中的?:在开头不会被捕获
\ /将匹配最后一个/
\ /(\ w +)将a /与重复一次或多次的单词字符匹配
|)将匹配字符串末尾的任何内容。
#4
0
I would use a negative lookahead (?!
) for this:
我会使用负向前瞻(?!):
$urls = array(
'http://example.com/foo',
'http://example.com/foo/',
'http://example.com/foo/bar',
'http://example.com/foobar'
);
foreach ($urls as $url) {
if (preg_match('#^http://example\.com/foo(?!bar)#', $url)) {
echo $url, " matches.\n";
} else {
echo $url, " does NOT match.\n";
}
}
// Output:
// http://example.com/foo matches.
// http://example.com/foo/ matches.
// http://example.com/foo/bar matches.
// http://example.com/foobar does NOT match.
#5
0
Javascript Regular Expression
Javascript正则表达式
https?://(example.com)/?/[^/]*/?(bar)?
Test here:(More Info..)
在这里测试:(更多信息..)
#1
4
Try this one:
试试这个:
^http://example\.com/foo(?:/.*)?$
#2
1
In your regex, the last /?
means an optional /
at the end. So /foobar
is also matched. Try this:
你的正则表达式,最后/?意味着可选/最后。所以/ foobar也匹配。试试这个:
http:\/\/example\.com\/foo($|\/.*)
#3
0
Try something like this:
尝试这样的事情:
http://example.com/foo(?:\/|/(\w+)|)
In regex form:
以正则表达式:
/http:\/\/example.com\/foo(?:\/|\/(\w+)|)/
This will match example.com/foo or example.com/foo/bar or example.com/foo/
这将匹配example.com/foo或example.com/foo/bar或example.com/foo/
Some explaination:
-
(foo|bar)
matches foo or bar -
(?:)
a group with the?:
in the begin will not been captured -
\/
will match a / at the end -
\/(\w+)
match a / with a word character who is repeated one or more times -
|)
will match nothing at the end of the string.
(foo | bar)匹配foo或bar
(?:)一个组中的?:在开头不会被捕获
\ /将匹配最后一个/
\ /(\ w +)将a /与重复一次或多次的单词字符匹配
|)将匹配字符串末尾的任何内容。
#4
0
I would use a negative lookahead (?!
) for this:
我会使用负向前瞻(?!):
$urls = array(
'http://example.com/foo',
'http://example.com/foo/',
'http://example.com/foo/bar',
'http://example.com/foobar'
);
foreach ($urls as $url) {
if (preg_match('#^http://example\.com/foo(?!bar)#', $url)) {
echo $url, " matches.\n";
} else {
echo $url, " does NOT match.\n";
}
}
// Output:
// http://example.com/foo matches.
// http://example.com/foo/ matches.
// http://example.com/foo/bar matches.
// http://example.com/foobar does NOT match.
#5
0
Javascript Regular Expression
Javascript正则表达式
https?://(example.com)/?/[^/]*/?(bar)?
Test here:(More Info..)
在这里测试:(更多信息..)