In QGIS, I have a string with the pattern (1 character)(1 character)(dot)(number with 1 or more digits)
.
How can I use regexp_replace()
to get the number only?
I tried regexp_replace('ab.27',('(\\D\\D(.))(\d)'),('\2'))
, but output is '?27'
, where I want '27'
.
How to mask the dot?
Edit
As I have a longer string then just what I presented here for sake of shortness as 'ab.27'
and this longer string might also contain digits, I must be able to identify the pattern mentioned above.
So the strings might look as (->
for what I want to achieve):
354ab.27 -> 27
ab.27fg87 -> 27
Best Answer
You can search for any char (
.*
), followed by a dot (\\.
), followed by a number that we capture ((\\d+)
) followed by any char. Replace with the captured number.or you can search for a dot followed by a number that we capture
Original answer for a pattern of
(1 character)(1 character)(dot)(number with 1 or more digits)
:Instead of searching for the exact pattern, you can search for what is not a number and replace it with an empty string:
or you can look for numbers only
But if you are always having the same pattern, you can just take all chars but the first 3 ones: