解析此或使用正则表达式

时间:2011-12-16 11:35:44

标签: regex parsing

我根本无法解决这个问题并且已经尝试了一段时间。我需要一个正则表达式,它将以下列方式解析数据:

假设我输入的格式如下:

www.google.com
www.google.com/
www.google.com/something

我需要一个正则表达式,它将上述三个URL(单独地)解析为最终结果:

www.google.com
www.google.com
www.google.com

但是,它需要匹配它们的方式基于以下内容:

Parse and return everything to the left of a "/" if one exists in the line
Parse and return the entire line if no "/" exists in the line

我是正则表达式的新手,所以虽然这可能很简单,但我无法理解。

2 个答案:

答案 0 :(得分:3)

尝试以下正则表达式:

[^/]*

答案 1 :(得分:1)

替换/.*没有任何东西。如果没有/根本没关系,因为在这种情况下正则表达式将不匹配。