正则表达式提取词在路径中

时间:2014-05-12 20:09:49

标签: regex path

我需要一个正则表达式来获取路径中的单词
例: (更新)

/var/log/rsyslog/apache/test1/2014/05/file1.log
/var/log/rsyslog/apache/test2/2014/05/file2.log
/var/log/rsyslog/apache/test3/2014/05/file3.log

输出应为

test1
test2
test3

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我不确定您使用哪种语言,一般来说,此正则表达式有效:

/\/(.*?)\.log/

正则表达式解释

/(.*?)\.log

Match the character “/” literally «/»
Match the regular expression below and capture its match into backreference number 1 «(.*?)»
   Match any single character that is not a line break character «.*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “.” literally «\.»
Match the characters “log” literally «log»