0

I have a list of input-strings as follows: "PlusContent" "PlusArchieve" "PlusContent1" "PlusArchieve1"

and so on, meaning that there are allways two entries that build up a topic {content, archieve}. Now I want to get the number (let´s call it postfix) at the end of that string. For this aim I used the following regex:

Regex r = new Regex(@"Plus.+?(\d*)$", RegexOptions.IgnoreCase);

Now I loop the matches (should be up to 2, the first is the whole string, second one the actual match if existing).

foreach (Match m in r.Matches(tester)) Console.WriteLine(m);

where tester is the current string within my list. But as result I allways get the whole string, but not its postfix. I tested the same regex on regexHero and it worked...

Any ideas what´s going whrong?

P.S.: This is the whole code:

List<string> words = new List<string> { "PlusContent", "PlusArchieve", "PlusContent1", "PlusArchieve1" };
foreach(string tester in words) {
    Regex r1 = new Regex(@"Plus.+?(\d*)$", RegexOptions.IgnoreCase);
    foreach (Match m in r1.Matches(tester)) Console.WriteLine(m);
}

But for some strange reason I get only the original strings from within the list.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • follow the link that I shared in my post and get the captured group only that is captured by parenthesis `(...)` – Braj Jul 28 '14 at 13:51

3 Answers3

2

Try with capturing groups and get the matched group from index 1.

\bPlus\D*(\d*)\b

DEMO

Read more about capturing group

Pattern explanation:

  \b                       the word boundary
  Plus                     'Plus'
  \D*                      non-digits (all but 0-9) (0 or more times (most))
  (                        group and capture to \1:
    \d*                      digits (0-9) (0 or more times (most))
  )                        end of \1
  \b                       the word boundary
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Well, I also tried out with index 1, but that gives me a NRE, as the resulting collection has only one single element (the whole string)... – MakePeaceGreatAgain Jul 28 '14 at 13:57
  • As I found out the regex itself was entirly correct, so all the "test-it" hints were not usefull at all... the problem obviously was accessing the groups. However, +1 for the second link (I actually figured it out by cruellays answer) – MakePeaceGreatAgain Jul 28 '14 at 14:08
  • I have clearly mentioned that use index 1. Since I am not a C# guy. I just know about a little bit about regex pattern that's why I shared you link for code. – Braj Jul 28 '14 at 14:09
1

user3218114's regex is correct. Since the numbers are captured using grouping, you need to access them using the Groups property like below:

List<string> words = new List<string> { "PlusContent", "PlusArchieve", "PlusContent1", "PlusArchieve1" };
foreach (string tester in words)
{
    Regex r1 = new Regex(@"\bPlus\D*(\d*)\b", RegexOptions.IgnoreCase);
    foreach (Match m in r1.Matches(tester)) Console.WriteLine(m.Groups[1]);
}

In this case, m.Group[0] is the original string content while m.Group[1] is the grouping specified in the regex.

Yuan Shing Kong
  • 674
  • 5
  • 15
0

May this will help with short Regex which will fetch numbers in any string as suffix.

\bPlus\D+(\d*)

Here's Demo

And if you're using Regex then you can get the numbers in $1 with above mentioned regular expression.

List<string> words = new List<string> { "PlusContent", "PlusArchieve", "PlusContent1", "PlusArchieve1" };
        foreach (string tester in words)
        {
            Regex r1 = new Regex(@"\bPlus\D+(\d*)", RegexOptions.IgnoreCase);
            foreach (Match m in r1.Matches(tester))
            {
                //Console.WriteLine(m);
                if (!string.IsNullOrEmpty(Regex.Replace(tester, @"\bPlus\D+(\d*)", "$1")))
                    Console.WriteLine(Regex.Replace(tester, @"\bPlus\D+(\d*)", "$1"));
            }
        }
Braj
  • 46,415
  • 5
  • 60
  • 76
Nirav Mehta
  • 6,943
  • 9
  • 42
  • 51
  • Where is prefix `Plus` is gone. The number should be treated as postfix not for all words. – Braj Jul 28 '14 at 13:56
  • Sent you the full code updates in your code package, could you please update the vote if the solution code works for you? P.S. I've already checked with console application. – Nirav Mehta Jul 28 '14 at 14:03