2

I am a newbie for programming, at this stage I am using an automation software, it supports c# and js.

Is it possible to search each line and replace a word?

Example data List name: A

Sample data a
Sample data b
Sample data c

To create a c# code so that when there is "a", it changes to x1

This one below is the most close, but it will remove that whole line and replace it with x1. My goal is only to replace a particular word.

If there can be an option to define multiple matches, that would be great.

a > x1
b > x2
c > x3

The code I found that does search and replace, however it remove the whole line that contains this particular match: The code below will remove whole line that contains a number, and replace it with 1 I found the code in this forum.

var sourceList = project.Lists["A"];  // define list name.

var parserRegex = new Regex("\\d{1,2}"); // it will match all numbers

lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++) // loop through each line.
    {
        if (parserRegex.IsMatch(sourceList[i])) // to check if there is a match
        {
            sourceList[i]="1"; // This code do the replacing job, but it replace the whole line, not the string.
        }
    }
}

With your guys' help, I think I have got what I wanted:

Here is my final code that is working for now. It is not perfect but works for my purpose.

My question still remains is that for replacing command, how to define replacing "a" means only to replace like

Turn "Sample data a" into "sample data x1" But not to do like "Sx1mple dx1tx1 x1".

Code:

var sourceList = project.Lists["A-Source"];  // define list name.

var parserRegex = new Regex("data"); // it will match all numbers

lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++) // loop through each line.
    {
     //   if (parserRegex.IsMatch(sourceList[i])) // to check if there is a match.  This line is commented out and it still works.
        {
            sourceList[i]= sourceList[i].Replace("a", "x1")
            .Replace("b","x2")
            .Replace("c","x3")
            .Replace("<p>","")
            .Replace("<strong>",""); // I added two other lines, to remove like p and strong tags, it works!
        }
    }
}

The replace pair's left part is the target, and the right part is the final replacing text.

In real examples, it can't just be "a", "b" or "c" because a is not only going to replace a, but also the "a" symbol in word like "data".

C# is powerful, thanks for the generous input!

stackmike
  • 73
  • 5
  • I'm not sure what data structure you're working on, but you can generally use string.Replace() to match a piece of a string and replace it with something else. It won't modify anything but the match. – Johannes Mols Jun 22 '21 at 21:23
  • Can `Sample data a` contains `b`? If so, should we transform `Sample data a b` to `Sample data x1 x2`? – aloisdg Jun 22 '21 at 21:24
  • And, should `Sample data a` get transformed to `Sx1mple dx1tx1 x1`? That's one way to interpret _"when there is 'a', it changes to x1"_ – Flydog57 Jun 22 '21 at 21:39
  • JohannesMols Thanks for the comment. The data are plain list. They are txt files. aloisdg Yes, multiple matches can be found in one single line. Sample data can be like "Sample data a b c" in this case it shall turn into "Sample data x1 x2 x3". No case match required. @Flydog57 Thanks for pointing out the possibility that a match can happen in a particular part of a word. In my case, it needs to match the whole word. So "a" must match "a", when it won't match data even there is a "a" there in the word. – stackmike Jun 22 '21 at 22:08

2 Answers2

0

Like Johannes mentioned in comments, in C# you can use String.Replace()

sourceList[i]= sourceList[i].Replace("a", "x1")
jgraff
  • 46
  • 5
  • Thanks for the reply. It does work! But I seem to missing a define parameter, because it will replace anything that contains the match string. All a will be replaced, even like data ,it becomes dx1tx1. How to control it to be only replace the whole word? – stackmike Jun 22 '21 at 22:22
  • https://stackoverflow.com/a/6143686/4762482 - see if this helps? – taylorswiftfan Jun 22 '21 at 23:41
0

This should work:

        var sourceList = project.Lists["A"];  // define list name.

        string pattern = @"(?'matched'\d*)";

        for (int i = 0; i < sourceList.Count; i++) // loop through each line.
        {
            foreach (Match m in Regex.Matches(sourceList[i], pattern))
            {
                Group g = m.Groups["matched"];
                if (!string.IsNullOrEmpty(g.Value))
                {
                    sourceList[i] = sourceList[i].Replace(g.Value, "newvalue");
                }
            }
        }
Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • Thanks @sekhar I tried it, there is no reaction though. What does this line mean? string pattern = @"(?'matched'\d*)"; Shall I replace "matched" with my intended replacing target? Are @"(? and \d)" part of a controlling parameter to make sure it only match that word? – stackmike Jun 22 '21 at 22:34
  • 'matched' is a named grouping, which will let you search using that later on. Yes, you can remove \d* with the intended regex. ?'matched' is microsoft regex, this regex may not work with other tools. – Sekhar Jun 22 '21 at 23:02