mercredi 6 mai 2015

Capture optional second group of digits (non-repeating) using regex

I have a huge dataset, where I am trying to extract a group of 4 digits. The problem is, sometimes there will be a preceding group of 4 digits that I -don't- want. These 2 groups will never be the same as each other.

Example:

String String 7777 Some more string
String 1234 7777 Some more string

In both of these situations, I want to extract ONLY 7777 (or whatever digit combination replaces it). There is no pattern to distinguish which number group will be in which position - any number from 0000 to 9999 can be in either first or second position.

If this were possible, I think it'd do what I want?

\b\d{4}{0,1}\s{0,1}(\d{4})\b

Optional 4 digits, optional space, capture 4 digits. But I've tried it, and some variations of it, but I can't get it to work!

Lookahead seems like a possible candidate, but I don't understand how to construct the pattern.

Aucun commentaire:

Enregistrer un commentaire