C# Regex append number after “$1” [duplicate]












-1
















This question already has an answer here:




  • Regex.Replace: How to use $groups followed by numbers

    3 answers




extern const BP_UINT8 g_SysMapDis_<DIST_N>;


I want to replace the 'DIST_N' with '0' or '1', like:



extern const BP_UINT8 g_SysMapDis_0;


So I do something like this



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(.+)(<DIST_N>)(.+)");
int n = 0;
string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret += REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, "$1" + n + "$3");


I only get this



$10;// should be "extern const BP_UINT8 g_SysMapDis_0;"


So how to append a number after "$1"?










share|improve this question













marked as duplicate by Wiktor Stribiżew c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 10:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • "should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

    – Anu Viswan
    Jan 1 at 7:13













  • @AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

    – Ansersion
    Jan 1 at 7:17











  • so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

    – Anu Viswan
    Jan 1 at 7:18













  • @AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

    – Ansersion
    Jan 1 at 7:20











  • I have updated my answer. Kindly check if that resolves your issue

    – Anu Viswan
    Jan 1 at 7:21
















-1
















This question already has an answer here:




  • Regex.Replace: How to use $groups followed by numbers

    3 answers




extern const BP_UINT8 g_SysMapDis_<DIST_N>;


I want to replace the 'DIST_N' with '0' or '1', like:



extern const BP_UINT8 g_SysMapDis_0;


So I do something like this



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(.+)(<DIST_N>)(.+)");
int n = 0;
string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret += REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, "$1" + n + "$3");


I only get this



$10;// should be "extern const BP_UINT8 g_SysMapDis_0;"


So how to append a number after "$1"?










share|improve this question













marked as duplicate by Wiktor Stribiżew c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 10:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • "should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

    – Anu Viswan
    Jan 1 at 7:13













  • @AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

    – Ansersion
    Jan 1 at 7:17











  • so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

    – Anu Viswan
    Jan 1 at 7:18













  • @AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

    – Ansersion
    Jan 1 at 7:20











  • I have updated my answer. Kindly check if that resolves your issue

    – Anu Viswan
    Jan 1 at 7:21














-1












-1








-1









This question already has an answer here:




  • Regex.Replace: How to use $groups followed by numbers

    3 answers




extern const BP_UINT8 g_SysMapDis_<DIST_N>;


I want to replace the 'DIST_N' with '0' or '1', like:



extern const BP_UINT8 g_SysMapDis_0;


So I do something like this



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(.+)(<DIST_N>)(.+)");
int n = 0;
string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret += REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, "$1" + n + "$3");


I only get this



$10;// should be "extern const BP_UINT8 g_SysMapDis_0;"


So how to append a number after "$1"?










share|improve this question















This question already has an answer here:




  • Regex.Replace: How to use $groups followed by numbers

    3 answers




extern const BP_UINT8 g_SysMapDis_<DIST_N>;


I want to replace the 'DIST_N' with '0' or '1', like:



extern const BP_UINT8 g_SysMapDis_0;


So I do something like this



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(.+)(<DIST_N>)(.+)");
int n = 0;
string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret += REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, "$1" + n + "$3");


I only get this



$10;// should be "extern const BP_UINT8 g_SysMapDis_0;"


So how to append a number after "$1"?





This question already has an answer here:




  • Regex.Replace: How to use $groups followed by numbers

    3 answers








c# regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 7:04









AnsersionAnsersion

116




116




marked as duplicate by Wiktor Stribiżew c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 10:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 10:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • "should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

    – Anu Viswan
    Jan 1 at 7:13













  • @AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

    – Ansersion
    Jan 1 at 7:17











  • so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

    – Anu Viswan
    Jan 1 at 7:18













  • @AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

    – Ansersion
    Jan 1 at 7:20











  • I have updated my answer. Kindly check if that resolves your issue

    – Anu Viswan
    Jan 1 at 7:21



















  • "should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

    – Anu Viswan
    Jan 1 at 7:13













  • @AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

    – Ansersion
    Jan 1 at 7:17











  • so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

    – Anu Viswan
    Jan 1 at 7:18













  • @AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

    – Ansersion
    Jan 1 at 7:20











  • I have updated my answer. Kindly check if that resolves your issue

    – Anu Viswan
    Jan 1 at 7:21

















"should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

– Anu Viswan
Jan 1 at 7:13







"should be "extern const BP_UINT8 g_SysMapDis_0;" that doesnt have a $1 ?

– Anu Viswan
Jan 1 at 7:13















@AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

– Ansersion
Jan 1 at 7:17





@AnuViswan, the $1 should be replace with "extern const BP_UINT8 g_SysMapDis_"

– Ansersion
Jan 1 at 7:17













so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

– Anu Viswan
Jan 1 at 7:18







so, is your expected output be "extern const BP_UINT8 g_SysMapDis_$10$3;" or just "extern const BP_UINT8 g_SysMapDis_0;"?

– Anu Viswan
Jan 1 at 7:18















@AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

– Ansersion
Jan 1 at 7:20





@AnuViswan "extern const BP_UINT8 g_SysMapDis_0;" is what I expect

– Ansersion
Jan 1 at 7:20













I have updated my answer. Kindly check if that resolves your issue

– Anu Viswan
Jan 1 at 7:21





I have updated my answer. Kindly check if that resolves your issue

– Anu Viswan
Jan 1 at 7:21












2 Answers
2






active

oldest

votes


















2














Your regex needs to be following as that is the string you are trying to replace.



"(<DIST_N>)"


Complete Code



Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(<DIST_N>)");
int n = 0;
string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret = REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, n.ToString() );


Output



extern const BP_UINT8 g_SysMapDis_0;


Additional Comment



Please note you could use a more simpler string.Replace in this case, instead of using Regex.



string ret = codeBlock.Replace("<DIST_N>",  n.ToString());





share|improve this answer


























  • Can't you use just codeBlock.Replace("<DIST_N>", 0)?

    – JohnyL
    Jan 1 at 8:56











  • I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

    – Anu Viswan
    Jan 1 at 8:59



















3














In this simple case you can use string replace instead of a regex:



string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
string ret = codeBlock.Replace("<DIST_N>", "0");


Specifically in your case, your regex replacements string was "$1" + n, which has the value "$10" - so it ended up looking for the 10th group. The solution is to use "${1}0" instead, separating the group from the following "0". For example:



var s = Regex.Replace("123abc",@"(.)","${1}0");


See also:




  • Working example

  • Microsoft Docs - Substitutions in Regular Expressions






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Your regex needs to be following as that is the string you are trying to replace.



    "(<DIST_N>)"


    Complete Code



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(<DIST_N>)");
    int n = 0;
    string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
    string ret = REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, n.ToString() );


    Output



    extern const BP_UINT8 g_SysMapDis_0;


    Additional Comment



    Please note you could use a more simpler string.Replace in this case, instead of using Regex.



    string ret = codeBlock.Replace("<DIST_N>",  n.ToString());





    share|improve this answer


























    • Can't you use just codeBlock.Replace("<DIST_N>", 0)?

      – JohnyL
      Jan 1 at 8:56











    • I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

      – Anu Viswan
      Jan 1 at 8:59
















    2














    Your regex needs to be following as that is the string you are trying to replace.



    "(<DIST_N>)"


    Complete Code



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(<DIST_N>)");
    int n = 0;
    string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
    string ret = REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, n.ToString() );


    Output



    extern const BP_UINT8 g_SysMapDis_0;


    Additional Comment



    Please note you could use a more simpler string.Replace in this case, instead of using Regex.



    string ret = codeBlock.Replace("<DIST_N>",  n.ToString());





    share|improve this answer


























    • Can't you use just codeBlock.Replace("<DIST_N>", 0)?

      – JohnyL
      Jan 1 at 8:56











    • I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

      – Anu Viswan
      Jan 1 at 8:59














    2












    2








    2







    Your regex needs to be following as that is the string you are trying to replace.



    "(<DIST_N>)"


    Complete Code



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(<DIST_N>)");
    int n = 0;
    string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
    string ret = REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, n.ToString() );


    Output



    extern const BP_UINT8 g_SysMapDis_0;


    Additional Comment



    Please note you could use a more simpler string.Replace in this case, instead of using Regex.



    string ret = codeBlock.Replace("<DIST_N>",  n.ToString());





    share|improve this answer















    Your regex needs to be following as that is the string you are trying to replace.



    "(<DIST_N>)"


    Complete Code



    Regex REGEX_CODE_BLOCK_DIST_N = new Regex(@"(<DIST_N>)");
    int n = 0;
    string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
    string ret = REGEX_CODE_BLOCK_DIST_N.Replace(codeBlock, n.ToString() );


    Output



    extern const BP_UINT8 g_SysMapDis_0;


    Additional Comment



    Please note you could use a more simpler string.Replace in this case, instead of using Regex.



    string ret = codeBlock.Replace("<DIST_N>",  n.ToString());






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 9:00

























    answered Jan 1 at 7:16









    Anu ViswanAnu Viswan

    5,6552526




    5,6552526













    • Can't you use just codeBlock.Replace("<DIST_N>", 0)?

      – JohnyL
      Jan 1 at 8:56











    • I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

      – Anu Viswan
      Jan 1 at 8:59



















    • Can't you use just codeBlock.Replace("<DIST_N>", 0)?

      – JohnyL
      Jan 1 at 8:56











    • I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

      – Anu Viswan
      Jan 1 at 8:59

















    Can't you use just codeBlock.Replace("<DIST_N>", 0)?

    – JohnyL
    Jan 1 at 8:56





    Can't you use just codeBlock.Replace("<DIST_N>", 0)?

    – JohnyL
    Jan 1 at 8:56













    I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

    – Anu Viswan
    Jan 1 at 8:59





    I agree, but the answer was more in line with the original OP. string replace would have been sufficient here. May be i could add it as comment

    – Anu Viswan
    Jan 1 at 8:59













    3














    In this simple case you can use string replace instead of a regex:



    string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
    string ret = codeBlock.Replace("<DIST_N>", "0");


    Specifically in your case, your regex replacements string was "$1" + n, which has the value "$10" - so it ended up looking for the 10th group. The solution is to use "${1}0" instead, separating the group from the following "0". For example:



    var s = Regex.Replace("123abc",@"(.)","${1}0");


    See also:




    • Working example

    • Microsoft Docs - Substitutions in Regular Expressions






    share|improve this answer




























      3














      In this simple case you can use string replace instead of a regex:



      string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
      string ret = codeBlock.Replace("<DIST_N>", "0");


      Specifically in your case, your regex replacements string was "$1" + n, which has the value "$10" - so it ended up looking for the 10th group. The solution is to use "${1}0" instead, separating the group from the following "0". For example:



      var s = Regex.Replace("123abc",@"(.)","${1}0");


      See also:




      • Working example

      • Microsoft Docs - Substitutions in Regular Expressions






      share|improve this answer


























        3












        3








        3







        In this simple case you can use string replace instead of a regex:



        string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
        string ret = codeBlock.Replace("<DIST_N>", "0");


        Specifically in your case, your regex replacements string was "$1" + n, which has the value "$10" - so it ended up looking for the 10th group. The solution is to use "${1}0" instead, separating the group from the following "0". For example:



        var s = Regex.Replace("123abc",@"(.)","${1}0");


        See also:




        • Working example

        • Microsoft Docs - Substitutions in Regular Expressions






        share|improve this answer













        In this simple case you can use string replace instead of a regex:



        string codeBlock = "extern const BP_UINT8 g_SysMapDis_<DIST_N>;";
        string ret = codeBlock.Replace("<DIST_N>", "0");


        Specifically in your case, your regex replacements string was "$1" + n, which has the value "$10" - so it ended up looking for the 10th group. The solution is to use "${1}0" instead, separating the group from the following "0". For example:



        var s = Regex.Replace("123abc",@"(.)","${1}0");


        See also:




        • Working example

        • Microsoft Docs - Substitutions in Regular Expressions







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 7:30









        KobiKobi

        108k34223265




        108k34223265















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith