-2

iso.3.6.1.4.1.2011.6.128.1.1.2.21.1.6.4194455040

For example, with SNMP OID I'd like to capture the last sequence numbers, which is 4194455040 and possible sometimes the second last, which is 6?

Or I need to iterate find how dots (.) I have in this string and capture these numbers?

Language: Python

TMoraes
  • 1,191
  • 4
  • 18
  • 38
  • What language are you using? Can't you just use its function for splitting a string at the `.` delimiters? – Barmar Sep 15 '20 at 18:59
  • It is python @Barmar – TMoraes Sep 15 '20 at 19:03
  • So use `oid.split('.')`. – Barmar Sep 15 '20 at 19:05
  • Use `r'(?<=\.)\d+$'` to match all characters between the last dot and the end of the string. [Demo](https://regex101.com/r/JdGGtA/1). Use `r'(?<=\.)\d+\.\d+$'` to match all characters between the penultimate dot and the end of the string. [Demo](https://regex101.com/r/JdGGtA/2). – Cary Swoveland Sep 15 '20 at 21:27

2 Answers2

0

I think that the best approach would be to capture all numbers separated by dots. Then you can decide how many of these numbers you pick according to your needs. The regex to do so is: /\.(\d+)/gm. This is assuming you really want/need to use a regex. Otherwise you could simply split the string.

Demo: https://regex101.com/r/OmzQcj/2/

Regex explained in reverse:

  • \d: Capture any digit (0 to 9)
  • \d+: At least once
  • (\d+): Save that into a group
  • \.(\d+): The group has a dot at the beginning

If you would prefer to capture only the last two batches of numbers you can instead use this Regex: https://regex101.com/r/evaF3X/1. This one will store these numbers in group 1 and group 2.

adelriosantiago
  • 6,469
  • 6
  • 30
  • 62
0

Use this regex when you need the last number.

\d+$

Working demo.

This regex will give you the second last one.

\d+(?=\.\d+$)

Working demo.

I'm using here lookaround operator. The first one is quite straight forward. It says to get one or more digits starting from the end of line.
The second one uses the lookahead operator. It says to get one or more digit that is followed by a .\d+$.

Maya
  • 1,225
  • 8
  • 18