簡體   English   中英

行中子字符串的正則表達式

[英]Regular expression for a substring from a line

我有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”

您必須從表達式中刪除^ ,例如A0[2-9] 由於結果不在表達式(^)的開頭。

在線演示

.NET小提琴演示

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);
    }
}

描述

(?:((?:[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
----------------------------------------------------------------------

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM