[英]REGEX: I have file name with extension and date in a string. How to extract only file name with extension not the date
我有 3 條記錄,其文件名帶有和不帶有日期,如下圖所示
我只需要提取文件名和擴展名而不是日期。 我在 REGEX 的幫助下使用了下面的代碼
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string a = Row.fullfilename;
Match match = Regex.Match(a, @"\D+\S\D+");
if (match.Success)
Row.filename = match.Groups[0].Value;
else
Row.filename = " ";
}
我得到如下圖的輸出
在第一個輸出記錄中,我沒有得到擴展名,而在其他記錄中,我在字符串的第一個和最后一個下划線
非常感謝您的幫助!
如果目標是刪除數字並保留所有其他字符和擴展名,則可以解決問題:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string a = Row.fullfilename;
string output = " ";
Match match = Regex.Match(a, @"[^\d{8}]+"); //removes only sets of 8 digits in a row
//Match match = Regex.Match(a, @"([^\d]+)"); //removes all digits
while (match.Success)
{
output += match.Groups[0].Value;
match = match.NextMatch();
}
Row.filename = output;
}
編輯:添加選項以連續刪除 8 位數字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.