chubs490
chubs490

Reputation: 3

How would I remove a certain amount of text after a string?

So, let's say there is :

MinimumPasswordAge = 4

I want to replace the 4, except the 4 will be a random number.

Or, how can i remove 1-2 characters after

MinimimPasswordAge =

BTW, this is all in a text file.

Upvotes: 0

Views: 77

Answers (1)

TheGeneral
TheGeneral

Reputation: 81593

There are many many ways to do this. However, here is a regex example

var input = "MinimumPasswordAge = 4";
var result = Regex.Replace(input, @"(?<=MinimumPasswordAge = )\d+", "345");
Console.WriteLine(result);

Output

NumValue = 345

Full Demo Here

Note :This is assuming you know how to read all the text from the text file, and subsiquently write to one using E.g. File.ReadAllText / File.ReadLines methods

Updated from Eric J's worthy comment

Use this pattern for white space tolerance

(?<=MinimumPasswordAge\s?=\s?)\d+

Upvotes: 1

Related Questions