簡體   English   中英

如何使用“正則表達式”提取文件名?

[英]How can I use 'Regex' to extract a file name?

我想從以下文本中提取文件名13572_BranchInformationReport_2012-06-28.zip -

1:30","/icons/def13572_BranchInformationReport_2012-06-28.zip","13572_BranchInformationReport_2012-06-28.zip",0,"184296","6 月 28 日

我使用的正則表達式代碼是:

var fileNames = from Match m in Regex.Matches(pageSource, @"[0-9]+_+[A-Za-z]+_+[0-9]+-+[0-9]+-+[0-9]+.+(acc|zip|app|xml|def|enr|exm|fpr|pnd|trm)")
                select m.Value;

哪個應該工作正常。

我錯過了什么?

你需要逃避。 在正則表達式的中間,因為。 匹配任何字符。

@"[0-9]+_+[A-Za-z]+_+[0-9]+-+[0-9]+-+[0-9]+\.+(acc|zip|app|xml|def|enr|exm|fpr|pnd|trm)"

試試下面的正則表達式:

[0-9]+_+[A-Za-z]+_+[0-9]+-+[0-9]+-+[0-9]+.+(acc|zip|app|xml|def|enr|exm|fpr|pnd|trm)(?=",")

您可以嘗試以下正則表達式:

\d{5}_\w*_\d{4}-\d{2}-\d{2}\.(acc|zip|app|xml|def|enr|exm|fpr|pnd|trm)

這將匹配任何:

  1. 以五位數開頭
  2. 然后是下划線
  3. 然后任意數量的字母或數字
  4. 然后是下划線
  5. 然后是日期部分:四位數字、破折號、兩位數、破折號,然后是最后兩位數字。
  6. 然后是一段
  7. 最后是擴展。

PowerShell 示例:

$text = '1:30","/icons/def13572_BranchInformationReport_2012-06-28.zip","13572_BranchInformationReport_2012-06-28.zip",0,"184296","Jun 28'

$regex = '\d{5}_\w*_\d{4}-\d{2}-\d{2}\.(acc|zip|app|xml|def|enr|exm|fpr|pnd|trm)'

$text -match $regex

$matches[0]

暫無
暫無

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

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