这是一个字符串: http://news.ycombinator.com/page?vasya=pupkin&b=b news.ycombinator.com/page news.ycombinator.com/page.php news.ycombinator.com/page
我正在提取带有页面的主机。所以我写了以下正则表达式:
([a-zA-Z0-9\.]*[a-zA-Z0-9]+[^\/][\.][a-zA-Z0-9\/\.]+)
这些返回给我(粗体):
http:// news.ycombinator.com/page?vasya = pupkin&b = b news.ycombinator.com/page news.ycombinator.com/page.php news.ycombinator.com/page
这不是我真正需要的。在使用以下字符串:的情况下,正则表达式应该看不到带有页面的主机http://news.ycombinator.com/page?vasya=pupkin&b=b
,因为它是一个链接,应区别对待。
应该被拒绝:
"http://news.ycombinator.com/page?vasya=pupkin&b=b", "http://news.ycombinator.com/page", "http://news.ycombinator.com/","http://news.ycombinator.com".
不应该被拒绝:
"news.ycombinator.com/page","news.ycombinator.com/page.php", "news.ycombinator.com/page/index", "news.ycombinator.com/page/index.php"
如何改进此正则表达式,以便它只能选择那些附近没有字字符的字符串部分?
我不确定您使用的是什么正则表达式,但是您实际上已经解决了自己的问题-您只需要正则表达式匹配整个单词即可。这将取决于您使用的程序,但这是一个guidleine(posix样式正则表达式):
([:space:][a-zA-Z0-9\.]*[a-zA-Z0-9]+[^\/][\.][a-zA-Z0-9\/\.]+[:space:])
or maybe ([:space:]([a-zA-Z0-9]*[\.\/])+[a-zA-Z0-9]+[:space:])
在第二个中,您必须确保内部组适用于非捕获组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句