【问题标题】:regex extract word in path正则表达式提取路径中的单词
【发布时间】:2014-06-30 08:13:19
【问题描述】:

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

/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

感谢您的帮助

【问题讨论】:

  • 您使用哪种语言?
  • 你是在 Linux 上做的吗?
  • 感谢 Sam 的回答,但这不是我想要接收的字符串

标签: regex path


【解决方案1】:

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

/\/(.*?)\.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»

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多