将字符串替换为目标格式

时间:2015-06-03 10:42:56

标签: .net regex vb.net string replace

有没有办法用regex或字符串替换

替换字符串
COND X > 49 300000 200000

预期格式为

COND(X > 49,300000,200000)

我目前的做法是string.Split(" ")并将其转换为列表并将大括号插入适当的索引。但我的方法的问题是字符串不是独立的,而是包含在更大的表达式中,有时候与whitespace中的COND ABC =进行比较。条件的右侧是空格。

3 个答案:

答案 0 :(得分:2)

单次通过是不可能实现的。我建议:

  1. 添加括号:(\p{Lu}+)\s*(\p{Lu}+)(.*)(替换为$1($2$3)
  2. 请参阅demo

    1. 然后,您可以使用正则表达式查找数字之间的所有空格,并用逗号替换它们:(?<=\d+)\s+(?=\d+)
    2. 请参阅demo(请参阅上下文选项卡)。

      这是一个有效的VB.NET代码:

      Dim strIn As String = "COND X > 49 300000 200000"
      Dim rx2 = New Regex("(\p{Lu}+)\s*(\p{Lu}+)(.*)")
      Dim result2 As String = rx2.Replace(strIn, "$1($2$3)")
      result2 = Regex.Replace(result2, "(?<=\d+)\s+(?=\d+)", ",")
      

      输出:

      enter image description here

      编辑:1-REGEX PASS:

      如果您在MatchEvaluator中使用Regex.Replace函数,我们可以确保只运行一次正则表达式。

      Dim str3 = "COND X > 49 300000 200000 778888"
          Dim rx3 = New Regex("(\p{Lu}+)\s*(\p{Lu}+.*?)(?:\s+(\d+))+")
          Dim result2 = rx3.Replace(str3, New MatchEvaluator(Function(m As Match)
                              If m.Groups(3).Captures.Count > 0 Then
                                   Return String.Format("{0}({1} {2})",
                                                     m.Groups(1).Value,
                                                     m.Groups(2).Value,
                                                     String.Join(",",
                                                                  m.Groups(3).Captures.Cast(Of Capture)().Select(Function(n) n.Value).ToArray()
                                                                 )
                                                     )
                               Else
                                   Return m.Value
                               End If
                               End Function))
      

      结果:

      enter image description here

答案 1 :(得分:2)

RegularExpression中设置您的捕获组,并在组之间粘贴适当的分隔符。

注意:这仅适用于您想要在其间插入逗号的3组数字。

Imports System
Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        Dim str As String = "COND X < 49 300000 200000"
        '^ Beginning of the line
        '([A-Z]+)\s* capture group 1 that will have any capital letters before the first space,
        '  but the space is not included in the group
        '([A-Z]\s*..?\s*) capture group 2 that will have X (or any single capital letter) plus
        '  a space, then any character plus a possible character (<=), then a space
        '(\d+)\s* capture group 3 & 4 that will have the first group of digits plus a space, but 
        '  the space is not included in the group
        '(\d+)$ capture group 5 that will have the last group of digits. $ End of line
        Dim pattern As String = "^([A-Z]+)\s*([A-Z]\s*..?\s*)(\d+)\s*(\d+)\s*(\d+)$"
        Console.WriteLine(Regex.Replace(str, pattern, "$1($2$3,$4,$5)"))
        Console.ReadLine()
    End Sub
End Module

结果:

COND(X > 49,300000,200000)

更新

如果您处理的是三组以上的数字,那么使用Regex.Match并构建字符串可以通过一次。

Imports System
Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        Dim str As String = "COND X >= 49 300000 200000 123456 456789"
        '([A-Z]+)\s* capture group 1 that will have any capital letters before the first space,
        '  but the space is not included in the group
        '([A-Z]\s*..?\s*) capture group 2 that will have X (or any single capital letter) plus
        '  a space, then any character plus a possible character (<=), then a space
        '(.+)\s* capture group 3 that will have the sets of digits with spaces in between
        Dim pattern As String = "([A-Z]+)\s*([A-Z]\s*..?\s*)(.+)"
        Dim groups As GroupCollection = Regex.Match(str, pattern).Groups
        Console.WriteLine("{0}({1}{2})", groups(1), groups(2), groups(3).Value.Replace(" ", ","))
        Console.ReadLine()
    End Sub
End Module

结果:

COND(X >= 49,300000,200000,123456,456789)

答案 2 :(得分:1)

您可以使用以下正则表达式进行检测:

list of cell

以及以下替换:

(.*)(\w\s?>[\s\d]+)