How do I replace [number] to number - 1 using Regex in C#?What is the difference between String and string in C#?Cast int to enum in C#How to validate an email address in JavaScript?How to replace a character by a newline in Vim?How do I enumerate an enum in C#?How to validate an email address using a regular expression?What are the correct version numbers for C#?How do you use a variable in a regular expression?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScript

Would this custom Sorcerer variant that can only learn any verbal-component-only spell be unbalanced?

How easy is it to start Magic from scratch?

Implement the Thanos sorting algorithm

Escape a backup date in a file name

How do we know the LHC results are robust?

How does Loki do this?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

How to run a prison with the smallest amount of guards?

Anatomically Correct Strange Women In Ponds Distributing Swords

Pole-zeros of a real-valued causal FIR system

Why does indent disappear in lists?

Sort a list by elements of another list

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

How does the UK government determine the size of a mandate?

How to write papers efficiently when English isn't my first language?

Is the destination of a commercial flight important for the pilot?

What is paid subscription needed for in Mortal Kombat 11?

Is there a korbon needed for conversion?

Is it appropriate to ask a job candidate if we can record their interview?

How do scammers retract money, while you can’t?

How do I find the solutions of the following equation?

How long to clear the 'suck zone' of a turbofan after start is initiated?

Two monoidal structures and copowering

How did Doctor Strange see the winning outcome in Avengers: Infinity War?



How do I replace [number] to number - 1 using Regex in C#?


What is the difference between String and string in C#?Cast int to enum in C#How to validate an email address in JavaScript?How to replace a character by a newline in Vim?How do I enumerate an enum in C#?How to validate an email address using a regular expression?What are the correct version numbers for C#?How do you use a variable in a regular expression?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScript













1















I have string values like A, something happened [1], something else [2], whatever [3] and want to replace it to A, something happened 0, something else 1, whatever 2.



So, basically replace [ with and ] with if there is a number between and then decrement the number between by one. Is this possible with Regex somehow?



So far I got this:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", @"$1");
Console.Write(result);


However, this gives me A, something happened 1, something else 2, whatever 3.



Is there an option to decrement the numbers by one?










share|improve this question
























  • @Peter B: Thanks for the edit, I did some wrong typing there...

    – FranzHuber23
    Mar 7 at 13:13
















1















I have string values like A, something happened [1], something else [2], whatever [3] and want to replace it to A, something happened 0, something else 1, whatever 2.



So, basically replace [ with and ] with if there is a number between and then decrement the number between by one. Is this possible with Regex somehow?



So far I got this:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", @"$1");
Console.Write(result);


However, this gives me A, something happened 1, something else 2, whatever 3.



Is there an option to decrement the numbers by one?










share|improve this question
























  • @Peter B: Thanks for the edit, I did some wrong typing there...

    – FranzHuber23
    Mar 7 at 13:13














1












1








1


1






I have string values like A, something happened [1], something else [2], whatever [3] and want to replace it to A, something happened 0, something else 1, whatever 2.



So, basically replace [ with and ] with if there is a number between and then decrement the number between by one. Is this possible with Regex somehow?



So far I got this:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", @"$1");
Console.Write(result);


However, this gives me A, something happened 1, something else 2, whatever 3.



Is there an option to decrement the numbers by one?










share|improve this question
















I have string values like A, something happened [1], something else [2], whatever [3] and want to replace it to A, something happened 0, something else 1, whatever 2.



So, basically replace [ with and ] with if there is a number between and then decrement the number between by one. Is this possible with Regex somehow?



So far I got this:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", @"$1");
Console.Write(result);


However, this gives me A, something happened 1, something else 2, whatever 3.



Is there an option to decrement the numbers by one?







c# regex replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 13:10









Peter B

13.6k52046




13.6k52046










asked Mar 7 at 13:06









FranzHuber23FranzHuber23

198318




198318












  • @Peter B: Thanks for the edit, I did some wrong typing there...

    – FranzHuber23
    Mar 7 at 13:13


















  • @Peter B: Thanks for the edit, I did some wrong typing there...

    – FranzHuber23
    Mar 7 at 13:13

















@Peter B: Thanks for the edit, I did some wrong typing there...

– FranzHuber23
Mar 7 at 13:13






@Peter B: Thanks for the edit, I did some wrong typing there...

– FranzHuber23
Mar 7 at 13:13













1 Answer
1






active

oldest

votes


















2














You may decrement the number in a match evaluator:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", m => $"int.Parse(m.Groups[1].Value)-1");
Console.Write(result); // => A, something happened 0, something else 1, whatever 2


See the C# demo



In case the number cannot be cast to int use int.TryParse:



var result = Regex.Replace(text, @"[(d+)]", m => 
int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);


For C# prior to C#7:



var result = Regex.Replace(text, @"[(d+)]", m => 
int number;
if (int.TryParse(m.Groups[1].Value, out number))

return $"number-1";

else

return m.Value;

);





share|improve this answer

























  • I will try this. I guess, that's what I was looking for :)

    – FranzHuber23
    Mar 7 at 13:12






  • 1





    @FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

    – Wiktor Stribiżew
    Mar 7 at 13:18











  • @Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

    – FranzHuber23
    Mar 7 at 13:21







  • 1





    @FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

    – Wiktor Stribiżew
    Mar 7 at 13:29











  • @Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

    – FranzHuber23
    Mar 7 at 13:51










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044523%2fhow-do-i-replace-number-to-number-1-using-regex-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You may decrement the number in a match evaluator:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", m => $"int.Parse(m.Groups[1].Value)-1");
Console.Write(result); // => A, something happened 0, something else 1, whatever 2


See the C# demo



In case the number cannot be cast to int use int.TryParse:



var result = Regex.Replace(text, @"[(d+)]", m => 
int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);


For C# prior to C#7:



var result = Regex.Replace(text, @"[(d+)]", m => 
int number;
if (int.TryParse(m.Groups[1].Value, out number))

return $"number-1";

else

return m.Value;

);





share|improve this answer

























  • I will try this. I guess, that's what I was looking for :)

    – FranzHuber23
    Mar 7 at 13:12






  • 1





    @FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

    – Wiktor Stribiżew
    Mar 7 at 13:18











  • @Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

    – FranzHuber23
    Mar 7 at 13:21







  • 1





    @FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

    – Wiktor Stribiżew
    Mar 7 at 13:29











  • @Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

    – FranzHuber23
    Mar 7 at 13:51















2














You may decrement the number in a match evaluator:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", m => $"int.Parse(m.Groups[1].Value)-1");
Console.Write(result); // => A, something happened 0, something else 1, whatever 2


See the C# demo



In case the number cannot be cast to int use int.TryParse:



var result = Regex.Replace(text, @"[(d+)]", m => 
int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);


For C# prior to C#7:



var result = Regex.Replace(text, @"[(d+)]", m => 
int number;
if (int.TryParse(m.Groups[1].Value, out number))

return $"number-1";

else

return m.Value;

);





share|improve this answer

























  • I will try this. I guess, that's what I was looking for :)

    – FranzHuber23
    Mar 7 at 13:12






  • 1





    @FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

    – Wiktor Stribiżew
    Mar 7 at 13:18











  • @Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

    – FranzHuber23
    Mar 7 at 13:21







  • 1





    @FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

    – Wiktor Stribiżew
    Mar 7 at 13:29











  • @Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

    – FranzHuber23
    Mar 7 at 13:51













2












2








2







You may decrement the number in a match evaluator:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", m => $"int.Parse(m.Groups[1].Value)-1");
Console.Write(result); // => A, something happened 0, something else 1, whatever 2


See the C# demo



In case the number cannot be cast to int use int.TryParse:



var result = Regex.Replace(text, @"[(d+)]", m => 
int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);


For C# prior to C#7:



var result = Regex.Replace(text, @"[(d+)]", m => 
int number;
if (int.TryParse(m.Groups[1].Value, out number))

return $"number-1";

else

return m.Value;

);





share|improve this answer















You may decrement the number in a match evaluator:



var text = "A, something happened [1], something else [2], whatever [3]";
var result = Regex.Replace(text, @"[(d+)]", m => $"int.Parse(m.Groups[1].Value)-1");
Console.Write(result); // => A, something happened 0, something else 1, whatever 2


See the C# demo



In case the number cannot be cast to int use int.TryParse:



var result = Regex.Replace(text, @"[(d+)]", m => 
int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);


For C# prior to C#7:



var result = Regex.Replace(text, @"[(d+)]", m => 
int number;
if (int.TryParse(m.Groups[1].Value, out number))

return $"number-1";

else

return m.Value;

);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 13:30

























answered Mar 7 at 13:09









Wiktor StribiżewWiktor Stribiżew

326k16147226




326k16147226












  • I will try this. I guess, that's what I was looking for :)

    – FranzHuber23
    Mar 7 at 13:12






  • 1





    @FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

    – Wiktor Stribiżew
    Mar 7 at 13:18











  • @Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

    – FranzHuber23
    Mar 7 at 13:21







  • 1





    @FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

    – Wiktor Stribiżew
    Mar 7 at 13:29











  • @Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

    – FranzHuber23
    Mar 7 at 13:51

















  • I will try this. I guess, that's what I was looking for :)

    – FranzHuber23
    Mar 7 at 13:12






  • 1





    @FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

    – Wiktor Stribiżew
    Mar 7 at 13:18











  • @Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

    – FranzHuber23
    Mar 7 at 13:21







  • 1





    @FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

    – Wiktor Stribiżew
    Mar 7 at 13:29











  • @Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

    – FranzHuber23
    Mar 7 at 13:51
















I will try this. I guess, that's what I was looking for :)

– FranzHuber23
Mar 7 at 13:12





I will try this. I guess, that's what I was looking for :)

– FranzHuber23
Mar 7 at 13:12




1




1





@FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

– Wiktor Stribiżew
Mar 7 at 13:18





@FranzHuber23 If you want you may replace int with long, just make sure you use what works for your scenario best.

– Wiktor Stribiżew
Mar 7 at 13:18













@Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

– FranzHuber23
Mar 7 at 13:21






@Wiktor Stribiżew I put your code into one line like that: var result = Regex.Replace(text, @"[(d+)]", m => int.TryParse(m.Groups[1].Value, out var number) ? $"number - 1" : m.Value);. Of course, if the value is a long, I can simply use the same with long.TryParse. Thank you for the fast reply.

– FranzHuber23
Mar 7 at 13:21





1




1





@FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

– Wiktor Stribiżew
Mar 7 at 13:29





@FranzHuber23 Yeah, it will work starting with C#7, since the out argument "can pass without its declaration and initialization".

– Wiktor Stribiżew
Mar 7 at 13:29













@Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

– FranzHuber23
Mar 7 at 13:51





@Wiktor Stribiżew Thank you for the additional information, I did not really look for the C# version as I'm always using the latest features but it's good to notice this to others.

– FranzHuber23
Mar 7 at 13:51



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044523%2fhow-do-i-replace-number-to-number-1-using-regex-in-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко