-1

我有一个字符串,我想捕获其中的数字,然后添加一个!

例如,我有一个电子邮件主题标题,上面写着“Re: Hello (1)”

我想捕获那个 1,然后将它提高 2,然后是 3,然后是 4,等等。我遇到的困难是考虑到不断增长的数字,一旦它变成 10 或 100,那个额外的数字就会杀死我当前的正则表达式。

      int replyno = int.Parse(Regex.Match(Subject, @"\([0-9]+\)").Value);
      replyno++;
      string Subject = Subject.Remove(Subject.Length - 3);
      TextBoxSubject.Text = Subject + "("+replyno+")";
4

3 回答 3

2

Re: Hello \([0-9]+\)

This matches the string "Re: Hello (1)" with any number of digits as the number, so it also matches "Re: Hello (100)".

Note that I have specifically used [0-9] and not \d to match a digit, because they are different. \d will match numeric characters in other languages, too. See this question and answer for more info: https://stackoverflow.com/a/6479605/1801

If you need to match other numeric characters, you can replace [0-9] with \d

于 2013-11-04T14:32:38.897 回答
1

您可以使用

\((\d+)\)

作为正则表达式。这将在内部捕获一个数字(),而不管数字大小如何+

于 2013-11-04T14:29:39.560 回答
0

将您的正则表达式更改为\d+. 这将帮助您支持多位数字。

于 2013-11-04T14:29:28.287 回答