【问题标题】:Find and replace inside regex result in Excel VB在 Excel VBA 中查找和替换正则表达式结果
【发布时间】:2017-09-22 19:44:31
【问题描述】:

在经历了很多挫折和阅读文档之后,我似乎仍然找不到运行正则表达式模式搜索并替换该模式电子表格中所有匹配项中的字符的方法。

我在一个单元格中有这个内容:

$test string[0] = "this is a test string."; $another test string[431] = "yet another test string";

这是我的正则表达式模式:

"\$(.*?)\["

输出:

test string, another test string

我需要找到所有匹配项并将空格替换为连字符,以便输出为:

test-string, another-test-string

关于如何做到这一点的任何想法?

提前致谢!

【问题讨论】:

  • 输出为:test-string, another-test-string,其余的被忽略?我几乎可以肯定您必须分两步完成此操作。
  • 基本上我只想对我在正则表达式中搜索的内容进行查找和替换。完整的输出将是:$test-string[0] = "this is a test string."; $another-test-string[431] = "yet another test string";

标签: regex excel vb.net replace find


【解决方案1】:

我猜你可以使用积极的前瞻,即:

ResultString = Regex.Replace(SubjectString, "(?<=\$.*?) (?=.*?\[)", "-")

【讨论】:

  • 那个正则表达式对我不起作用......我错过了什么吗?这是一个活生生的例子:i.imgur.com/4JrtF6z.png"> 这是我在 www.regex101.com 编辑器中输入的用于简单复制粘贴的文本:正则表达式 (?&lt;=\$.*?) (?=.*?\[) 测试字符串 $test string[0] = "testing a string"; $string number 2[231] = "Testing yet another string. More words"; 玩了很多,阅读了更多文档,但仍然无法解决问题。
  • 在您的示例中,字符串位于 2 行不同的行中。到了我家的时候去看看。
  • 抱歉,慢慢习惯了这个非所见即所得的编辑器。谢谢你的帮助! :)
  • @Haddock-san:如果您打算在 Excel VBA 代码中使用它,由于后视,它将无法工作。甚至是无限宽度的向后看。这仅适用于 .NET(以及更多少数引擎)。因此,在 VB.NET 中,它可以工作。
猜你喜欢
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
  • 2018-09-28
  • 2015-03-25
  • 2011-06-16
  • 1970-01-01
相关资源
最近更新 更多