2014-10-09 46 views
0

我不是在正则表达式的字符串最大的专家,所以我失去了什么这一个应该在这里找到:这个正则表达式会检测到什么?

@"\\[(.*?)\\]\\((\\S+)(\\s+(\"|\')(.*?)(\"|\'))?\\)" 

我试图让降价模式去为一个应用程序。任何人都可以帮我用这种模式应该找到什么?

回答

1

正则表达式似乎与超链接的Markdown格式匹配。特别是那些直接指定链接并且不使用引用样式的链接。

[linked text](http://www.example.com/hyperlink "optional tooltip") 

捕获组1包含链接的文本。
捕获组2包含超链接。
捕获组4包含可选工具提示。

下面是例如链接到这个问题,我们有和其他没有提示:

的源以上2条超链接:

- [What will this regular expression detect?](https://stackoverflow.com/questions/26285433/what-will-this-regular-expression-detect "What will this regular expression detect?") (try hovering your mouse over this one) 
- [What will this regular expression detect?](https://stackoverflow.com/questions/26285433/what-will-this-regular-expression-detect) 

您可以尝试将上面的示例文本与您的正则表达式匹配。一旦你看到结果就会变得清晰。这是一个demo on regex101

(我在上面的demo中写过\[(.*?)\]\((\S+)(\s+("|')(.*?)("|'))?\),因为它是正则表达式引擎看到的)

0
\\[(.*?)\\]\\((\\S+)(\\s+(\"|\')(.*?)(\"|\'))?\\)

见ICU用户指南Regular Expressions

当需要\字符逃避他们自己必须在字符串中的转义正则表达式运算符。

 
\\[  matchs [ 
(  starts capture group 1 
    .*?  matches anything 0 or more times as few times as possible. 
)  ends capture group 1 
\\]  matches ] 
\\(  matches (
(  starts capture group 2 
    \\S+  matches all non-space characters 
)  ends capture group 2 
(  starts capture group 3 
    \\s+  matches one or more space characters 
    (  starts capture group 4 
     \"|\' matches a " or ' 
    )  ends capture group 4 
    (  starts capture group 5 
     .*?  matches anything 0 or more times as few times as possible 
    ))  ends capture group 5 
    (  starts capture group 6 
     \"|\' matches a " or ' 
    )  ends capture group 6 
)?  ends capture group 3 of zero or one matches 
\\)  matches)