简体   繁体   中英

How to remove a specific part of string with wildcard?

Currently, I use:

Variabls:

 int recordCount = 5;
 Header = "Index"; // Can also be "Starting Index"

Header:

 Header = Header.Split(' ')[0] + " (" + recordCount + ")";

Changes:

 Index (5)

To:

 Index (6)

When I want to replace the Header with a new Header, I use the above, but the problem is that when I start using more than one word in the Header it removes the rest of the Header Name. ie When it says Starting Index: it only shows Starting .

Can I use Regex to simply look for the value inbetween the parenthesis and replace it with another variable?

Regex re = new Regex(@"\(\w+\)");
string input = "Starting Index: (12asd)";
string replacement = "12ddsa";
string result = re.Replace(input, replacement);

If you need to perform more complex replacements (ie if the replacement depends on captured value between the braces), you'll have to stick with Regex.Match method

Update: with Match thing quickly get ugly :)

 Regex re = new Regex(@"^(.*)\((\w+)\)\s*$");
 string input = "Starting Index: (12)";
 var match = re.Match(input);

 string target = match.Groups[2].Value;
 //string replacement = target + "!!!!"; // general string operation
 int autoincremented = Convert.ToInt32(target) + 1; // if you want to autoincrement

 string result = String.Format("{0}: ({1})", match.Groups[1].Value, autoincremented);

If you need to replace a number of these systematically (and the algorithm requires the original value), then remember that Regex.Replace() can accept a method that will return the replaced value. Here's an example that will increment all integers enclosed in parens:

string s1 = "Index (5) and another (45) and still one more (17)";

string regex = @"\((\d+)\)";

string replaced = Regex.Replace(s1,regex,m => "("+(Convert.ToInt32(m.Groups[1].Value)+1).ToString()+")");
// Result: Index (6) and another (46) and still one more (18)

The method takes a regex match object and returns a replacement string. I used a lambda method here but your regex and the replacement method can each be as complex as needed.

you can also go with this way:

string sample = "Index (5) Starting Index(0) and Length (6)";
string content = Regex.Replace(sample, @"(?<=\()\d+(?=\))", m => (int.Parse(m.Value) + 1).ToString());

This pattern will look for any number of digits wrapped with round brackets and will be advanced to 1.

Here No need to append additional brackets, since they were not captured during match.

You could use this pattern

\[\((\d+)\).*?\]

to match the number between the parenthesis, and after that you could replace that number with whatever you want

var mg = Regex.Match( "Starting Index:(10)", @"\[\((\d+)\).*?\]");

if (mg.Success)
{
    var num = mg.Groups[1].Value; // num == 10
}

After that

headerString = headerString.Replace("10", "11");

\\((\\d+)\\) this will suits better

and will replace numbers in that case "asdq(wdq)wdqwd (12)"

int dynamicNumber = 6;

string pattern = string.Format("({0})", dynamicNumber);

string data = "My Header 6:";

Console.WriteLine (Regex.Replace(data,pattern, "!!!")); // My Header !!!:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM