Need regex expression in C# to extract 2 digits from the last but 2 digits.
For example consider the string "QWEREIA89RR". Here I want to extract "89"
Need regex expression in C# to extract 2 digits from the last but 2 digits.
For example consider the string "QWEREIA89RR". Here I want to extract "89"
Here is a approach with linq
string input = "Q1WEREIA89RR";
string result = string.Concat(input.Where(char.IsDigit).Reverse().Take(2).Reverse());
use the regex ([0-9]+)
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string input = "Q1WEREIA89RR";
var matches = Regex.Matches(input, @"([0-9]+)", RegexOptions.Compiled);
if (matches.Count>0)
{
var all = string.Join("",matches.SelectMany(x => x.Value));
int numOfChars = 2;
var last2 = string.Join("", all.Skip(all.Length - numOfChars).Take(numOfChars));
Console.WriteLine(last2);
}
Console.ReadLine();
}
}
}