-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

Shinomoto Asakura
  • 1,473
  • 7
  • 25
  • 45

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
  • 7,762
  • 7
  • 38
  • 71
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+$.

Tsubasa
  • 1,389
  • 11
  • 21