来自行的子字符串的正则表达式

时间:2016-06-07 22:27:58

标签: c# regex

我有一条XX,VV,A01,A02,A03,A11,A12,A13,A14,B11,B12,B13,ZZ,DD

我需要一个正则表达式

  1. 如果我在我的行中找到A01,A02,A03或A11,A12,A13,A14,我必须更换为" AA"
  2. 如果我找到B11,B12,B13,我必须用" BB"
  3. 我尝试过使用

    Dim dict As New Scripting.Dictionary 
    
    ' add the items to be removed
    ' dictionaries take key and value, but we are 
    ' only interested in the key here. So the
    ' value could really be anything.
    c.Add "name1" 1
    c.Add "name2" 1
    
    Worksheets("sheet4").Activate 
    With Worksheets("sheet3").PivotTables(1) 
     For i = 1 To .PivotFields.Count 
       For j = 1 To .PivotFields(i).PivotItems.Count
         If c.Exists(.PivotFields(i).PivotItems(j).Name) then
           .PivotFields(i).PivotItems(j).Visible = False
         End If
       Next 
     Next 
    End With
    

    它没有用 - 所以基本上如果我有A02,A03,A04,A05,A06,A07或A10,A11,A12 ....... A15,我必须更换为" AA&# 34;

2 个答案:

答案 0 :(得分:1)

您必须从表达式^中移除A0[2-9]。由于结果不在表达式的开头(^)。

<强> Online Demo

.NET Fiddle Demo

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;

public static class Sample1
{
    public static void Main()
    {
        var sampleInput = "XX,VV,A01,A02,A03,A11,A12,A13,A14,B11,B12,B13,ZZ,DD";
        var results = Regex.Replace(sampleInput, "A0[2-9]|A1[0-5]", "AA");
        Console.WriteLine("Line: {0}", results);
    }
}

答案 1 :(得分:1)

描述

(?:((?:[AB](?=0[2-9]|1[0-5])))[0-9]{2}(?:(?=,\s*\1),|))*

替换为: $1$1

Regular expression visualization

此正则表达式将执行以下操作:

  • 查找以A02 - A15B02 - B15
  • 连续逗号分隔的内容
  • 使用AABB
  • 替换整个游戏

实施例

现场演示

https://regex101.com/r/gN8aP6/1

示例文字

XX,VV,A01,A02,A03,A11,A12,A13,A14,B11,B12,B13,ZZ,DD

样本匹配

XX,VV,A01,AA,BB,ZZ,DD

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        [AB]                     any character of: 'A', 'B'
----------------------------------------------------------------------
        (?=                      look ahead to see if there is:
----------------------------------------------------------------------
          0                        '0'
----------------------------------------------------------------------
          [2-9]                    any character of: '2' to '9'
----------------------------------------------------------------------
         |                        OR
----------------------------------------------------------------------
          1                        '1'
----------------------------------------------------------------------
          [0-5]                    any character of: '0' to '5'
----------------------------------------------------------------------
        )                        end of look-ahead
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    [0-9]{2}                 any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
----------------------------------------------------------------------
        ,                        ','
----------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        \1                       what was matched by capture \1
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      ,                        ','
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------