使用PCRE从字符串中提取尾部

时间:2013-10-21 12:40:09

标签: regex url fragment pcre

我正在寻找一个PCRE正则表达式匹配模式,我可以使用它在C语言中提取字符串的尾部片段。我的预期效果是提取字符串“ en ”之后的字符串,后面可以紧跟任何内容,有或没有斜杠“ / ”。如果“ en ”后面的第一个字符为斜线,请在返回捕获的字符串之前忽略或修剪它。在较小的情况下,输入字符是纯ASCII。

input-string        match   captured-string
---------------------------------------
english/japan       no
en                  yes
en/                 yes
en/japan            yes     japan
en//japan           yes     japan
en/japan/tokyo      yes     japan/tokyo
en//japan/tokyo     yes     japan/tokyo
en//                yes

提前谢谢!

2 个答案:

答案 0 :(得分:1)

^en(?:/+(.+)|/?)$

^    #beginning of line
  en    #'en' literal
   (?:    #beginning of a not capturing group
     /+(.+)    #'/' one or more times + 'any' character one or more times (capturing group)
     |    # OR
     /?    #'/' zero or one time    
   )    #closing not capturing group
$    #end of line

答案 1 :(得分:0)

echo "en//japan/tokyo" | sed -rn 's;^en($|/+(.*));\2;p'