【问题标题】:Regex: Match a set of filenames separately from a string正则表达式:与字符串分开匹配一组文件名
【发布时间】:2016-03-03 04:36:27
【问题描述】:

我用谷歌搜索了很多,得到了多个答案,但在我的情况下都没有。此正则表达式应处理的示例字符串是

"Purging file summary_t.dat after pre_summary.csv, data.dat clogged"

Regex.Replace("regex", "$fileName") 应该给出这个

"Purging file $fileName after $fileName, $fileName clogged"

我尝试过的正则表达式是

^[\w,\s-]+\.[A-Za-z]{3}$

只匹配第一个文件名

(?:[^\\:]+\\)*((?:[^:\\]+)\.\w+)

这个匹配所有内容,包括多个文件名之间的文本,所以我只得到一个 $filename 整行

注意:字符串是通用的,所以我真的不知道在哪里可以得到字符串中的文件名

【问题讨论】:

    标签: c# asp.net regex


    【解决方案1】:

    试试这个

    (\w+\.\w+)
    

    https://regex101.com/r/wK3cK7/1

    输出:

    MATCH 1
    1.  [13-26] `summary_t.dat`
    MATCH 2
    1.  [33-48] `pre_summary.csv`
    MATCH 3
    1.  [50-58] `data.dat`
    

    【讨论】:

      【解决方案2】:

      这也必须有效

      \w+\.\w+
      

      \w = Any letter, + = one or more, \. = followed by a dot (escape the . meaning, else only . means 0 or more) 这是演示的链接。 http://regexr.com/3cu60

      【讨论】:

      • ▲ for remove [],我的坏习惯
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多