【问题标题】:How can I find File name in string which start with # Using RegEx如何在以# Using RegEx 开头的字符串中找到文件名
【发布时间】:2015-03-31 03:01:04
【问题描述】:

我有字符串:

" testfile.txt #testfile.txt
  My new message
Filename1.doc #filename.doc"

如何找到以“#”开头的文件名并将其从字符串中删除

【问题讨论】:

  • 你的预期输出是什么?
  • 我的预期输出是:“testfile.txt 我的新消息 Filename1.doc”
  • 您的输入是否包含换行符?试试Regex.Replace(str, @"#\S+", "");
  • 是的!我的输入包含换行符,它应该保留在输出中。感谢您的回复。
  • 试试Regex.Replace(str, @"#\S+", "");Regex.Replace(str, @"#.*", "");

标签: c# regex winforms


【解决方案1】:

试试这个:

var pattern = @"\#[\w,\s-]+\.[A-Za-z]+\s*";
var result = Regex.Replace(
            "testfile.txt #testfile.txt My new message Filename1.doc #filename.doc",
            pattern, string.Empty);

结果:

 "testfile.txt  My new message Filename1.doc "

【讨论】:

  • 嗨,尼尔,它对我有用。谢谢你的帮助!!
  • 很高兴帮助你^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 2019-08-11
  • 2012-12-04
  • 1970-01-01
  • 2019-08-06
  • 2020-03-15
  • 1970-01-01
相关资源
最近更新 更多