【问题标题】:Regular expression for a substring from a line行中子字符串的正则表达式
【发布时间】:2016-06-07 22:27:57
【问题描述】:

我有一条线 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”替换

我尝试过使用

if (Regex.IsMatch(Value, "^A0[2-9]")|| Regex.IsMatch(Value, "^A1[0-5]"))

它没有用——所以基本上如果我有 A02,A03,A04,A05,A06,A07 或 A10,A11,A12....... A15 ,我必须用“AA”替换

【问题讨论】:

  • 从你给定的输入字符串XX,VV,A01,A02,A03,A11,A12,A13,A14,B11,B12,B13,ZZ,DD,你想要的输出字符串是什么样的?

标签: c# regex


【解决方案1】:

说明

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

替换为: $1$1

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

  • 查找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
----------------------------------------------------------------------

【讨论】:

  • 您好,谢谢您的回复。您能否提供一个正则表达式来替换 A 而不是 B 的 XX,VV,A02,A03,A11,A12,A13,A14,ZZ,DD 替换为 XX,VV,AA,ZZ,DD 对于其中一些,它说无法识别的转义序列。谢谢
  • @Kathy 和你给定的字符串 XX,VV,A01,A02,A03,A11,A12,A13,A14,ZZ,DD 所需的字符串是什么样的?
  • 我想要这个 XX,VV,AA,ZZ,DD 来自 XX,VV,A02,A03,A11,A12,A13,A14,ZZ,DD
  • 我对正则表达式很陌生。请帮忙!
  • 所以你的整个问题有点令人困惑,但(?:(((?:[AB](?=[0-9]{2}))))[0-9]{2}(?:(?=,\s*\2),|))* 这个正则表达式会将你的示例字符串转换为XX,VV,AA,ZZ,DD。当然,如果您输入的字符串还包含B01,B02...,那么它也会替换那些。要限制这种行为,您可以简单地将[AB] 部分中的字母更改为您想要匹配的字母。另见working demo
【解决方案2】:

您必须从表达式中删除^,例如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);
    }
}

【讨论】:

  • 非常感谢您的回复。但这是将整个 sampleInput 替换为 AA 并将其他的排除在外。我不希望这样,它应该只替换 A 并保持其他的原样。
猜你喜欢
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多