Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Note:I have written the piece of code for replacing the values with in the [] brackets.
Issue: HSA:3269is getting replaced with value1,but rest of the values inside [] not getting replaced with value2,value3.


C#
using System.Linq;
using System.Xml.Linq;
using System.Text.RegularExpressions;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var pattern = @"\[(.*?)\]";
            var query = "H1-receptor antagonist [HSA:3269] heelo [PATH:hsa04080(3269)] world [xyz]";
            var matches = Regex.Matches(query, pattern);
            string ReplaceableValue = query;
            string[] array = { "value1", "value2", "value3" };
            foreach (Match m in matches)
            {
                System.Console.WriteLine(m.Groups[1]);
                for (int i = 0; i <= m.Length; i++)
                {

                    ReplaceableValue = ReplaceableValue.Replace(m.Value.ToString(), array[i]);
                }

            }
            System.Console.WriteLine(ReplaceableValue);

            System.Console.ReadLine();




        }
    }
}
Posted

1 solution

You already have the solution. Just replace the Group with your Value. Hope it will help you.
C#
int i=0;
foreach (Match m in matches)
{
    System.Console.WriteLine(m.Groups[1]);
    //for (int i = 0; i <= m.Length; i++)
    {
       //ReplaceableValue = ReplaceableValue.Replace(m.Value.ToString(), array[i]);
       ReplaceableValue = ReplaceableValue.Replace(m.Groups[1].ToString(), array[i++]);
    }
}
System.Console.WriteLine(ReplaceableValue);
 
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