我有以下正则表达式(粗略匹配看起来像URLS的东西)
(https?://\S*)
但是,这是为了在句子中提取URL,因此尾随点可能可能句子的结尾而不是URL的合法部分。
让捕获组忽略尾随句号,逗号,冒号,分号等的神奇咒语是什么?
(我知道匹配的URL是一个噩梦,这只需要支持松散匹配,因此非常简单的正则表达式)
这是我的测试字符串:
lorem http://www.example.com lorem https://example.com lorem
http://www.example.com.
lorem https://example.com.
这应匹配所有example.com实例。
(我正在用Expresso和.NET测试它)
带尾随点和新行的测试结果:
Expected string length 62 but was 64. Strings differ at index 31.
Expected: "<a href="http://www.example.com">http://www.example.com</a>.\n\r"
But was: "<a href="http://www.example.com.\n">http://www.example.com.\n</a>\r"
------------------------------------------^
示例代码
public class HyperlinkParser
{
private readonly Regex _regex =
new Regex(
@"(https?://\S*[^\.])");
public string Parse(string original)
{
var parsed = _regex.Replace(original, "<a href=\"$1\">$1</a>");
return parsed;
}
}
示例测试
[TestFixture]
public class HyperlinkParserTests
{
private readonly HyperlinkParser _parser = new HyperlinkParser();
private const string NO_HYPERLINKS = "dummy-text";
private const string FULL_URL = "http://www.example.com";
private const string FULL_URL_PARSED = "<a href=\"" + FULL_URL + "\">" + FULL_URL + "</a>";
private const string FULL_URL_TRAILING_DOT = FULL_URL + ".";
private const string FULL_URL_TRAILING_DOT_PARSED = "<a href=\"" + FULL_URL + "\">" + FULL_URL + "</a>.";
private const string TRAILING_DOT_AND_NEW_LINE = FULL_URL_TRAILING_DOT + "\n\r";
private const string TRAILING_DOT_AND_NEW_LINE_PARSED = FULL_URL_TRAILING_DOT_PARSED + "\n\r";
private const string COMPLEX_TEXT = "Leading stuff http://www.example.com. Other stuff.";
private const string COMPLEX_TEXT_PARSED = "Leading stuff <a href=\"http://www.example.com\">http://www.example.com</a>. Other stuff.";
[TestCase(NO_HYPERLINKS, NO_HYPERLINKS)]
[TestCase(FULL_URL, FULL_URL_PARSED)]
[TestCase(FULL_URL_TRAILING_DOT, FULL_URL_TRAILING_DOT_PARSED)]
[TestCase(TRAILING_DOT_AND_NEW_LINE, TRAILING_DOT_AND_NEW_LINE_PARSED)]
[TestCase(COMPLEX_TEXT, COMPLEX_TEXT_PARSED)]
public void Parsing(string original, string expected)
{
var actual = _parser.Parse(original);
Assert.That(actual, Is.EqualTo(expected));
}
}
答案 0 :(得分:2)
试试这个,它禁止将dot作为最后一个字符:
(https?://\S*[^.])
E.g。在cygwin下,有egrep:
$ cat ~/tmp.txt
lorem http://www.example.com lorem https://example.com lorem
http://www.example.com.
lorem https://example.com.
$ cat ~/tmp.txt | egrep -o 'https?://\S*[^.]'
http://www.example.com
https://example.com
http://www.example.com
https://example.com
(-o
选项告诉egrep只打印匹配。)