locked
Replace text in a string between two points RRS feed

  • Question

  • User-1188570427 posted

    Hello,

    What is the best way to replace text in a string between two points?

    Example:

    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    I want to replace all "f"s between visible and width with "t"s?

    Thursday, August 2, 2018 2:31 PM

Answers

  • User-369506445 posted

    in my sample showa rest of your string

    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e
    
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    I updated the last post, maybe you have not seen it, please try below code

     var text = "page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e";
        var pattern = "visible(.*)width";
        Regex regex = new Regex(pattern);
        var fetch = regex.Match(text);
        string result = fetch.Groups[1].ToString().Replace("f", "t");
    
        result = Regex.Replace(text, pattern, "visible" + result + "width");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 2, 2018 3:37 PM

All replies

  • User-369506445 posted

    hi

    you can use <g class="gr_ gr_4 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="4" data-gr-id="4">Regex</g> expression

     var text = "page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e";
        var pattern = "visible(.*)width21";
        Regex regex = new Regex(pattern);
        var fetch = regex.Match(text);
        string result = fetch.Groups[1].ToString().Replace("f", "t");
    
        result = Regex.Replace(text, pattern, "visible" + result + "width21");

    result

    befor Replace
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e
    
    
    after Replace
    
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    Thursday, August 2, 2018 2:58 PM
  • User-1188570427 posted

    hi

    you can use Regex expression

     var text = "page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e";
        var pattern = "visible(.*)width21";
        Regex regex = new Regex(pattern);
        var fetch = regex.Match(text);
        string result = fetch.Groups[1].ToString().Replace("f", "t");
    
        result = Regex.Replace(text, pattern, "visible" + result + "width21");

    result

    befor Replace
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e
    
    
    after Replace
    
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    Thanks for the help.

    The end of width might be any number though.

    If I change the patter to this:

    var pattern = "visible(.*)width(.*)";

    This is my result:

    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|

    It is missing the rest?

    Thursday, August 2, 2018 3:21 PM
  • User-369506445 posted

    in my sample showa rest of your string

    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e
    
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    I updated the last post, maybe you have not seen it, please try below code

     var text = "page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e";
        var pattern = "visible(.*)width";
        Regex regex = new Regex(pattern);
        var fetch = regex.Match(text);
        string result = fetch.Groups[1].ToString().Replace("f", "t");
    
        result = Regex.Replace(text, pattern, "visible" + result + "width");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 2, 2018 3:37 PM
  • User-1188570427 posted

    in my sample showa rest of your string

    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e
    
    page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|t4|t5|t6|t7|t8|t9|t10|t11|t12|t13|t14|t15|t16|t17|t18|t19|t20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e

    I updated the last post, maybe you have not seen it, please try below code

     var text = "page1|size10|hierarchy21|0|-1|1|-1|2|-1|3|-1|4|-1|5|-1|6|-1|7|-1|8|-1|9|-1|10|-1|11|-1|12|-1|13|-1|14|-1|15|-1|16|-1|17|-1|18|-1|19|-1|20|-1|visible21|t0|t1|t2|t3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|f16|f17|f18|f19|f20|width21|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e|e";
        var pattern = "visible(.*)width";
        Regex regex = new Regex(pattern);
        var fetch = regex.Match(text);
        string result = fetch.Groups[1].ToString().Replace("f", "t");
    
        result = Regex.Replace(text, pattern, "visible" + result + "width");

    Awesome, fixing the pattern and then me adding the code that you updated appears to have given me the results I need. 

    YOU ARE AWESOME!

    Thursday, August 2, 2018 3:41 PM
  • User-369506445 posted

    I’m glad I could help

    Thursday, August 2, 2018 3:48 PM