Click here to Skip to main content
15,845,681 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I have A02 ----- A05 or A10 ---- A15 it should not convert to ABC

example..
If i have BDC, ASD, DFS, A02, A04, A13, QAS,ZXA

I need to replace it with BDC, ASD, DFS, ABC, ABC, ABC, QAS,ZXA
It might not be that I have aa of A02, A03... A05 and A10, A11, .... A15. My sample line can have anything from A02----A05 or from A10 --- A15. Please help with a regular expression for this using Regex.match

Thanks in advance

What I have tried:

I tried Regex.IsMatch(val, "A0[2-5]")) | (Regex.IsMatch(val, "A1[0-5]")
But what this is doing is replacing whole line to only ABC. But I do not want that. I want all the others existing with just replacing the ones starting with A0.
Posted
Updated 8-Jun-16 12:40pm

If you want to play with RegEx, here is a couple tools to help debugging.
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
.NET Regex Tester - Regex Storm[^]
 
Share this answer
 
Try using Regex's Replace():
C#
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication16
{
  class Program
  {
    static readonly Regex Pattern = new Regex("A0[2-5]|A1[0-5]", RegexOptions.Compiled);
    
    static void Main(string[] args)
    {
      string input = "BDC, ASD, DFS, A02, A04, A13, QAS,ZXA";
      string replacement = "ABC";
      Console.WriteLine(input);
      string output = Pattern.Replace(input, replacement);
      Console.WriteLine(output);
      Console.ReadLine();
    }
  }
}

Gives:
BDC, ASD, DFS, A02, A04, A13, QAS,ZXA
BDC, ASD, DFS, ABC, ABC, ABC, QAS,ZXA
 
Share this answer
 
v2
You should try experimenting with Expresso:
Expresso - A Tool for Building and Testing Regular Expressions[^]

Download the most recent version here:
Download Expresso[^]

As a general advice, in this case you might want to look into regex groups.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900