Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What RegEx string will find the last (rightmost) group of digits in a string?

Tags:

c#

regex

Looking for a regex string that will let me find the rightmost (if any) group of digits embedded in a string. We only care about contiguous digits. We don't care about sign, commas, decimals, etc. Those, if found should simply be treated as non-digits just like a letter.

This is for replacement/incrementing purposes so we also need to grab everything before and after the detected number so we can reconstruct the string after incrementing the value so we need a tokenized regex.

Here's examples of what we are looking for:

  • "abc123def456ghi" should identify the'456'
  • "abc123def456ghi789jkl" should identify the'789'
  • "abc123def" should identify the'123'
  • "123ghi" should identify the'123'
  • "abc123,456ghi" should identify the'456'
  • "abc-654def" should identify the'654'
  • "abcdef" shouldn't return any match

As an example of what we want, it would be something like starting with the name 'Item 4-1a', extracting out the '1' with everything before being the prefix and everything after being the suffix. Then using that, we can generate the values 'Item 4-2a', 'Item 4-3a' and 'Item 4-4a' in a code loop.

Now If I were looking for the first set, this would be easy. I'd just find the first contiguous block of 0 or more non-digits for the prefix, then the block of 1 or more contiguous digits for the number, then everything else to the end would be the suffix.

The issue I'm having is how to define the prefix as including all (if any) numbers except the last set. Everything I try for the prefix keeps swallowing that last set, even when I've tried anchoring it to the end by basically reversing the above.

like image 417
Mark A. Donohoe Avatar asked Jan 05 '12 12:01

Mark A. Donohoe


People also ask

What is the RegEx pattern for end of string?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

How do you find the last number in a string?

To get the number from the end of a string, call the match() method, passing it the following regular expression [0-9]+$ . The match method will return an array containing the number from the end of the string at index 0 .

How do I get the last digit of a string in Python?

The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.


1 Answers

How about:

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

then increment the second group and concat 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 until the first digit of last group of digits before the end of the string.

or if you can use named group

^(?<prefix>.*?)(?<number>\d+)(?<suffix>\D*)$
like image 51
Toto Avatar answered Oct 04 '22 02:10

Toto