Frage

I want to get a number after second dot in a string like that : 4.5.3. Some kind of question ? but input string might look like this as well 41.53.32. Some kind of question ? so im aiming for 3 in the first example and 32 in second example.

I'm trying to do it with

(?<=(\.\d\.))[0-9]+ 

and it works on 1st example, but when im trying to add (?<=(\.\d+\.))[0-9]+ it doesn't work at all.

War es hilfreich?

Lösung

If there is always a dot after the final number then you can use the following expression:

\d+(?=\.(?:[^\d]|$))

This will match one or more digits \d+ which are followed by a dot . then something that is either not a number [^\d] of the end-of-string $, i.e. (?=\.(?:[^\d]|$)).

Regex101 Demo

Andere Tipps

If you use PERL or PHP, you can try this pattern:

(?:\d+\.){2}\K\d+

The simplest complete answer is probably something like this:

(?<=^(?:[^.]*\.){2})\d+

If you're at all worried about performance, this one will be slightly faster:

^(?:[^.]*\.){2}(\d+)

This one will capture the desired value in capturing group 1.

If you are using an engine that doesn't support variable-length lookbehind, you'll need to use the second version.

If you wish, you can replace [^.] with \d, to only match digits.

(\d+.\d+.)\K\d+

Match digits dot digits dot digits, with the first section as a group not selected.

(?:(?:.*\.)?){2}(\d+)

the following regex should work for your use case.

check it out here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top