簡體   English   中英

正則表達式:與字符串分開匹配一組文件名

[英]Regex: Match a set of filenames separately from a string

我在Google上搜索了很多,得到了多個答案,但在我的情況下卻無濟於事。 此正則表達式應使用的示例字符串是

"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

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

嘗試這個

(\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`

這也必須工作

\w+\.\w+

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM