If a any given string, at the end contains "(" followed by a number, + ")", i want to increase that value by one. If not Ill just add a "(1)".
Ive tried with something like string.Contains(), but since the value within () can be diffrent i don't know how to always search like this and get the number.
To find a parentheses enclosed number at the end of a string, and increase b 1, try this:
Regex.Replace(yourString, @"(?<=\()\d+(?=\)$)", match => (int.Parse(match.Value) + 1).ToString());
Explanation:
(?<=\\()
is a positive look-behind, which matches an open bracket, but does not include it in the match result.
\\d+
matches one or more digits.
(?=\\)$)
is a positive look-ahead, which matches a closing bracket at the end of the string.
To add a number if none is present, test the match first:
string yourString = "A string with no number at the end";
string pattern = @"(?<=\()\d+(?=\)$)";
if (Regex.IsMatch(yourString, pattern))
{
yourString = Regex.Replace(yourString, pattern, match => (int.Parse(match.Value) + 1).ToString());
}
else
{
yourString += " (1)";
}
You can try regular expressions : Match
and Replace
the desired fragment, eg
using System.Text.RegularExpressions;
...
string[] tests = new string[] {
"abc",
"def (123)",
"pqr (123) def",
"abs (789) (123)",
};
Func<string, string> solution = (line) =>
Regex.Replace(line,
@"\((?<value>[0-9]+)\)$",
m => $"({int.Parse(m.Groups["value"].Value) + 1})");
string demo = string.Join(Environment.NewLine, tests
.Select(test => $"{test,-20} => {solution(test)}"));
Console.Write(demo);
Outcome:
abc => abc # no numbers
def (123) => def (124) # 123 turned into 124
pqr (123) def => pqr (123) def # 123 is not at the end of string
abs (789) (123) => abs (789) (124) # 123 turned into 124, 789 spared
If we put
Func<string, string> solution = (line) => {
Match m = Regex.Match(line, @"\((?<value>[0-9]+)\)$");
return m.Success
? line.Substring(0, m.Index) + $"({int.Parse(m.Groups["value"].Value) + 1})"
: line + " (1)";
};
Edit: If we want to put (1)
if we haven't any match we can try Match
and replace matched text:
abc => abc (1)
def (123) => def (124)
pqr (123) def => pqr (123) def (1)
abs (789) (123) => abs (789) (124)
string s = "sampleText";
string pattern = "[(]([0-9]*?)[)]$";
for (int i = 0; i < 5; i++)
{
var m = Regex.Match(s, pattern);
if (m.Success)
{
int value = int.Parse(m.Groups[1].Value);
s = Regex.Replace(s, pattern, $"({++value})");
}
else
{
s += "(1)";
}
Console.WriteLine(s);
}
If I understand correctly you have strings such as :
string s1 = "foo(12)"
string s2 = "bar(21)"
string s3 = "foobar"
And you want to obtain the following:
IncrementStringId(s1) == "foo(13)"
IncrementStringId(s2) == "bar(22)"
IncrementStringId(s3) == "foobar(1)"
you could accomplish this by using the following method
public string IncrementStringId(string input)
{
// The RexEx pattern is looking at the very end of the string for any number encased in paranthesis
string pattern = @"\(\d*\)$";
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
if (match.Success)
if (int.TryParse(match.Value.Replace(@"(", "").Replace(@")", ""), out int index))
//if pattern in found parse the number detected and increment it by 1
return Regex.Replace(input, pattern, "(" + ++index + ")");
// In case the pattern is not detected add a (1) to the end of the string
return input + "(1)";
}
Please make sure you are using System.Text.RegularExpressions namespace that includes Regex class.
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.