MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

regex for extracting a number

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.

Upvotes: 0

Views: 259

Answers (3)

Nirav Mehta
Nirav Mehta

Reputation: 7063

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"));
            }
        }

Upvotes: 0

Braj
Braj

Reputation: 46841

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

Upvotes: 2

Yuan Shing Kong
Yuan Shing Kong

Reputation: 674

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.

Upvotes: 1

Related Questions