x Hint: Register, if you want to hide ads.
x Did you like it? Well, then please consider making a donation :)

How to get numbers 1st, 2nd, 3rd, 4th from text

You can use capturing groups to organize and parse an expression. A non-capturing group has the first benefit, but doesn't have the overhead of the second. You can still say a non-capturing group is optional, for example. Say you want to match numeric text, but some numbers could be written as 1st, 2nd, 3rd, 4th... If you want to capture the numeric part, but not the (optional) suffix you can use a non-capturing group. ([0-9]+)(?:st|nd|rd|th)? That will match numbers in the form 1, 2, 3... or in the form 1st, 2nd, 3rd,... but it will only capture the numeric part.

Tags: #digits, #first, #get digits, #get numbers, #numbers, #parsing text, #second, #third. Views: 2503. stackoverflow, 7/12/2015
Link 1: http://www.systemtextregularexpressions.com/m9/how-to-get-numbers-1st-2nd-3rd-4th-from-text
Link 2: http://www.systemtextregularexpressions.com/m9 (short)

Length: 0b, Line: 1, Cursor: 0

RegexOptions

IsMatch=true


#ValueIndexLengthGroupsCaptures
012303Skip Navigation Links.Skip Navigation Links.
145643Skip Navigation Links.Skip Navigation Links.
27882Skip Navigation Links.Skip Navigation Links.
31st113Skip Navigation Links.Skip Navigation Links.
42nd153Skip Navigation Links.Skip Navigation Links.
53th193Skip Navigation Links.Skip Navigation Links.
6234233Skip Navigation Links.Skip Navigation Links.
Count: 7, Time: 00:00:00

Share (without adding to database):

Source code

0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
//.NET Regex.Match Online Tester: www.systemtextregularexpressions.com

//Namespace
using System.Text.RegularExpressions;

//Regex Pattern
string pattern = @"([0-9]+)(?:st|nd|rd|th)?";

//Regex example #1
Regex r = new Regex(@"([0-9]+)(?:st|nd|rd|th)?");
Match m1 = r.Match("input text");
MatchCollection mc1 = r.Matches("input text");

//Regex example #2
Match m2 = Regex.Match("input text", @"([0-9]+)(?:st|nd|rd|th)?");
MatchCollection mc2 = Regex.Matches("input text", @"([0-9]+)(?:st|nd|rd|th)?");
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
'.NET Regex.Match Online Tester: www.systemtextregularexpressions.com

'Namespace
Imports System.Text.RegularExpressions

'Regex Pattern
Dim pattern As String = "([0-9]+)(?:st|nd|rd|th)?"

'Regex example #1
Dim r As Regex = New Regex("([0-9]+)(?:st|nd|rd|th)?")
Dim m1 As Match = r.Match("input text")
Dim mc As MatchCollection = r.Matches("input text")

'Regex example #2
Dim m2 As Match = Regex.Match("input text", "([0-9]+)(?:st|nd|rd|th)?")
Dim mc2 As MatchCollection = Regex.Matches("input text", "([0-9]+)(?:st|nd|rd|th)?")

Home     Match     Replace     Split     Help     Library     Contacts     netregex rss

Made by Koshevoy Dmitry [8Bytes],

© 2014-2018 Ukraine, Nikolaev.