Which line of RegEx will find the last (rightmost) group of digits in the line?

Looking for a string of regular expressions that will allow me to find the rightmost (if any) group of digits embedded in the string. We only care about related numbers. We do not care about the sign, commas, decimals, etc. Those, if found, simply should be considered as numbers without a letter.

This is for replacement / increase purposes, so we also need to capture everything before and after the detected number, so that we can restore the string after increasing the value, so that we need a token regular expression.

Here are examples of what we are looking for:

  • "abc123def456ghi" must identify "456"
  • "abc123def456ghi789jkl" must identify "789"
  • 'abc123def' must identify '123'
  • '123ghi' must identify '123'
  • "abc123,456ghi" must identify "456"
  • "abc-654def" must identify "654"
  • "abcdef" must not return any match

As an example of what we want, it would be like starting with β€œItem 4-1a,” extracting β€œ1” with everything before being prefix and everything after the suffix. Then, using this, we can generate the values ​​"Item 4-2a", "Item 4-3a" and "Item 4-4a" in the code loop.

Now, if I were looking for the first set, it would be easy. I would just find the first adjacent block of 0 or more digits without a prefix, then a block of 1 or more adjacent digits for a number, then everything else will be a suffix to the end.

The problem is how to define a prefix as including all (if any) numbers except the last. Everything that I try to use for the prefix continues to swallow the last set, even when I tried to snap it to the end, basically by flipping above.

+7
source share
5 answers

What about:

^(.*?)(\d+)(\D*)$ 

then enlarge the second group and connect all 3.

Explanation:

 ^ : Begining of string ( : start of 1st capture group .*? : any number of any char not greedy ) : end group ( : start of 2nd capture group \d+ : one or more digits ) : end group ( : start of 3rd capture group \D* : any number of non digit char ) : end group $ : end of string 

The first capture group will match all characters up to the first digit of the last group of digits to the end of the line.

or if you can use a named group

 ^(?<prefix>.*?)(?<number>\d+)(?<suffix>\D*)$ 
+13
source

Try the following regex:

 (\d+)(?!.*\d) 

Explanation:

 (\d+) # One or more digits. (?!.*\d) # (zero-width) Negative look-ahead: Don't find any characters followed with a digit. 

EDIT (OFFTOPIC question):: This answer is incorrect, but this question has already been answered in other posts to avoid deleting it. I will use the same regular expression in another way, for example, in Perl you can use it to get the same result as in C# (incrementing the last digit):

 s/(\d+)(?!.*\d)/$1 + 1/e; 
+5
source

You can also try a slightly simpler version:

 (\d+)[^\d]*$ 
+3
source

This should do it:

 Regex regexObj = new Regex(@" # Grab last set of digits, prefix and suffix. ^ # Anchor to start of string. (.*) # $1: Stuff before last set of digits. (?<!\d) # Anchor start of last set of digits. (\d+) # $2: Last set of one or more digits. (\D*) # $3: Zero or more trailing non digits. $ # Anchor to end of string. ", RegexOptions.IgnorePatternWhitespace); 
+1
source

How about not using Regex. Here's a snippet of code (for the console)

 string[] myStringArray = new string[] { "abc123def456ghi", "abc123def456ghi789jkl", "abc123def", "123ghi", "abcdef","abc-654def" }; char[] numberSet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] filterSet = new char[] {'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z','-'}; foreach (string myString in myStringArray) { Console.WriteLine("your string - {0}",myString); int index1 = myString.LastIndexOfAny(numberSet); if (index1 == -1) Console.WriteLine("no number"); else { string mySubString = myString.Substring(0,index1 + 1); string prefix = myString.Substring(index1 + 1); Console.WriteLine("prefix - {0}", prefix); int index2 = mySubString.LastIndexOfAny(filterSet); string suffix = myString.Substring(0, index2 + 1); Console.WriteLine("suffix - {0}",suffix); mySubString = mySubString.Substring(index2 + 1); Console.WriteLine("number - {0}",mySubString); Console.WriteLine("_________________"); } } Console.Read(); 
+1
source

All Articles