C# Regex append number after “$1” [duplicate]
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"?
c# regex
marked as duplicate by Wiktor Stribiżew
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.
|
show 2 more comments
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"?
c# regex
marked as duplicate by Wiktor Stribiżew
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
|
show 2 more comments
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"?
c# regex
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
c# regex
asked Jan 1 at 7:04


AnsersionAnsersion
116
116
marked as duplicate by Wiktor Stribiżew
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
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
|
show 2 more comments
"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
|
show 2 more comments
2 Answers
2
active
oldest
votes
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());
Can't you use justcodeBlock.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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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());
Can't you use justcodeBlock.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
add a comment |
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());
Can't you use justcodeBlock.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
add a comment |
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());
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());
edited Jan 1 at 9:00
answered Jan 1 at 7:16


Anu ViswanAnu Viswan
5,6552526
5,6552526
Can't you use justcodeBlock.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
add a comment |
Can't you use justcodeBlock.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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 1 at 7:30


KobiKobi
108k34223265
108k34223265
add a comment |
add a comment |
"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