Matching a single numeric digit after the second period of a string

Question:

I have a series of data that I would like to extract a single digit from. I will be using python to execute the regex.
This previous post looked promising, but I couldn’t get anything working as of yet. More specifically I would like the first numeric digit after the second "." of each string.

This is my attempted regex: [^.][^.]?([0-9])

The below example includes test data with the targeted number in bold. I plan to use this to iterate through a table, so it only needs to work for each individual string, not all of them at once.

Example data:

AR0.RRPG.105

AR6.TR0.500

AR0.GGF0.2FEH

AR0.DER.M15

Asked By: Ryan Bobo

||

Answers:

^[^.]*.[^.]*.[^.]*?([0-9])

  • First as many non-dot characters as possible up to the first dot
  • Same up to the second dot
  • Finally as few non-dot characters as possible, up to the first digit, which is captured
Answered By: LMD

If you can have a long string of non digits first following after the last dot (before reaching the first digit), you can also exclude matching both a dot and a digit in the negated character class [^.d]* to prevent some backtracking.

^[^.]*.[^.]*.[^.d]*(d)

See a regex demo.

Note that you don’t have to escape the . in the character class, and the using [^.] can also match a newline.

Answered By: The fourth bird
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.