2

I have 2 strings, one is main string and other is pattern string to be replaced. I want to replace the some part of main string with pattern string only if particular pattern is matched in main string.

Example:

string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
string pattern =    "[lmn].[def].[ijk]";

i want final string as

[lmn].[def].[ijk] = [abc].[def].[ijk]

i.e. if only 2 part is there in string than only replace not for 3 parts

i am using:

mainstring = mainstring.Replace("[def].[ijk]",pattern);

but it replaces as,

[lmn].[def].[ijk] = [abc].[lmn].[def].[ijk]
                          ^-------+-------^
                                  |
                                  +-- don't replace here

but I want as

[lmn].[def].[ijk] = [abc].[def].[ijk]

EDIT: Additional rule for the replacement:

You can touch left hand side or right hand side but the pattern should be alone without anything at before or after.

3 Answers 3

1

I am trying to follow this rule:

i.e. if only 2 part is there in string than only replace not for 3 parts

(interpreting it as replace only if the matching pattern is not inbetween other parts, or: neither preceded nor follwed by a .)

This regular expression should satisfy this rule above:

string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
// will replace only right side
//string mainstring = "[abc].[def].[ijk] = [def].[ijk]"; 

string replacement = "[lmn].[def].[ijk]";

string pattern = @"(?<!\.)\[def\].\[ijk\](?!\.)";

string output = Regex.Replace(mainstring, pattern, replacement);

EDIT:

if you need to transform you basic pattern into a regex format replace both parentheses:

string searchPattern  = "[def].[ijk]";

searchPattern = searchPattern.Replace("[", @"\[").Replace("]", @"\]");

and place the result in between the look ahead and look behind pattern:

string pattern = @"(?<!\.)" + searchPattern + @"(?!\.)";

Explanation:

(?<!\.): the pattern is called negative lookahead. It will match only if the preceding character is not a . (dot)

(?!\.): the pattern is called negative lookbehind. It will match only if the following character is not a . (dot)

the replacement of [ ] to \[ \] is necessary because regex treats the parentheses as an interval or group and matches every character between the parentheses. Like [0-9] will match any digit between 0 and 9. But if you use an escape sequence @"\[0-9\]" it will take the parentheses as a character and match the pattern "[0-9]". I hope it became a little clearer

Sign up to request clarification or add additional context in comments.

13 Comments

i can split the problem is the split character is not fix. i.e. it can have =,!=, <,>,<> and other operators. so using particular character string wont help also it is not fixed which part should be replaced, i.e. in example you r replacing on [0]th index, but my pattern can be on any index
@Arc then you really should specify exactly how the rules are for replacement. If there is no clear rule, or at least a couple of contraints this will be very difficult to solve.
Following are some rules,to be followed : 1. The split characters are not fixed, any character can be there (like =,!=,<,>,<>) 2. the patterns position is not fixed, it can be either left or right side of split character. And split characters are only above specified, it can be +,-,*,/,&,(,),And, Or, Not,=,<>,<,<=,>=,>. also all the text functions, date function, conversion function, text function, summary functions, logical operators
@Arc which rule determines that only the left handside in your example should be replaced but not the right handside?
That is what i am saying, it can be on any side, either left or right side :(
|
0

does this work for you?

 string mainstring = "[def].[ijk] = [abc].[def].[ijk]";

            string leftFillWith = "[lmn].[def].[ijk]";

            string pattern = @"((\[lmn\]).)?((\[def\]).)?(\[ijk\])? =";

          var expected=  Regex.Replace(mainstring, pattern,match=> leftFillWith+" =" );

if only two parts are accepted:

  string mainstring = "[def].[ijk] = [abc].[def].[ijk]";

            string leftFillWith = "[lmn].[def].[ijk]";

           // string pattern = @"((\[lmn\]).)?((\[def\]).)?(\[ijk\])? ="; //if any number of parts is accepted on left


          string  pattern = @"(\[def\]\.\[ijk\])(\s)+="; //if only two parts are accepted

            string expected = Regex.IsMatch(mainstring,pattern)? Regex.Replace(mainstring, pattern, match => leftFillWith + " ="):mainstring;

9 Comments

can you add little explanation what is happening? is i am little confused in last two lines of your snippet
pattern searches on left side of mainstring for the bracketed units. You can see there are questionmarks in the pattern, so it means that they are optional: not requiered as there may be not constant amount of unit on left side. So this pattern finds the left-sided combination and replaces it with leftFillWith by adding "=" sign at the end with " "(space) off course.
@Arc your pattern was [lmn]. It searches for "l", "m", "n", but not for "lmn". These brackets are for classes ranges, but not for units. I guess you confused just because there lots of parentheses. But it helps as "combines".
But how can i solve my issue? brackets are there in string. What i want to do is just replace only if [def].[ijk] and not if [abc].[def].[ijk] in entire string
Any suggestions?
|
0

Not good but work;

       string mainstring = "[def].[ijk] = [abc].[def].[ijk]";
       string pattern = "[lmn].[def].[ijk]";
        string search ="[def].[ijk]";
        string mainstring3 = "";
       string[] mainstring2 = mainstring.Split(' ');
       for (int i = 0; i < mainstring2.Length; i++)
       {
           if (mainstring2[i] == search)
           {
               mainstring2[i] = pattern;
           }
           if (i < mainstring2.Length - 1)
           {
               mainstring3 += mainstring2[i] + " ";
           }
           else
           {
               mainstring3 += mainstring2[i];
           }
       }
       mainstring = mainstring3;
    }

6 Comments

string to replace will not only [def].[lkj], so what i did is var rege = @"\b"+stringtosearch+"\b";, this results in \b[def].[ijk], and this pattern is not present in original string
\b[def].[ijk]\b, this is what i am getting
if you want to capture [ as a character you need to use @"\[... other wise regex will interpret it as any character between the parentheses
But how can i solve my issue? brackets are there in string. What i want to do is just replace only if [def].[ijk] and not if [abc].[def].[ijk] in entire string. Any suggestions?
this will not match if the expression is standing alone on the right hand side: "[abc].[def].[ijk] = [def].[ijk]"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.