Retrieve characters at a given position

I am trying to find a function that will extract characters at a specific position within a string. For example, I have a long file name with a date in it, and I want to get only the date:

'LT50420331984221PAC00_B7.tif' 

and I want only the part "1984221". I came up with a complex function, but wondered if there was a more elegant solution.

+6
source share
2 answers

If you know the exact position of the date in your string, you can use

 substr('LT50420331984221PAC00_B7.tif', 10, 16) 
+13
source

For instance:

 gsub('(.*)([0-9]+{7})[AZ].*','\\2','LT50420331984221PAC00_B7.tif') "1984221" 

Here I assume that the date is 7 digits before the capital letter.

+3
source

Source: https://habr.com/ru/post/950590/


All Articles