解析字符串中的twitter短链接

时间:2012-06-16 18:12:06

标签: regex

我正在尝试解析'bit.ly/KATlrT'

之外的链接asdfs asdf as dfa sdf a bit.ly/KATlrT f sf sd f dsfdsa

这是正则表达式使用但它不匹配?

bit?/S+

3 个答案:

答案 0 :(得分:3)

(bit\.ly\/\S+)
^\ /^^\/^^\/^^
| | ||| ||| ||
| | ||| ||| |`- End first capture group
| | ||| ||| `-- Match 1 or more of previous (any non-space character)
| | ||| ||`---- Match any non-space character
| | ||| |`----- Match '/' literally because of previous escape character
| | ||| `------ Escape next special character
| | ||`-------- Match 'ly' literally
| | |`--------- Match '.' literally because of previous escape character
| | `---------- Escape next special character
| `------------ Match 'bit' literally
`------------- Start first capture group

<强> See it in action.


您在问题中提供的说明:

bit?/S+
\/^^^^^
| |||||
| ||||`- repeat previous ('S') 1 or more times
| |||`-- match 'S' literally
| ||`--- this is not escaped, and will cause problems
| |`---- previous is optional ('t')
| `----- match 't' literally 
`------- match 'bi' literally

如果您想了解有关正则表达式的更多信息,www.regular-expressions.info是一个很好的资源,RegexrRubular是测试正则表达式的绝佳工具。

答案 1 :(得分:0)

由于我不懂语言,我将发布Java正则表达式的答案(主要基于Perl正则表达式)。有很多语言遵循或多或少的Perl的正则表达式语法。

"bit\\.ly/[a-zA-Z]+"

答案 2 :(得分:0)

它可能不匹配,因为您忘记了\S,因此它会查找文字S。在大多数语言中,您还需要.而不是?

尝试bit\.\S+bit\.ly\/\S+