user2632308
user2632308

Reputation: 129

Extract characters at a set position

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

'LT50420331984221PAC00_B7.tif'

and I want only the '1984221' portion. I've come up with a complicated function, but was wondering if there is a more elegant solution.

Upvotes: 12

Views: 24197

Answers (2)

agstudy
agstudy

Reputation: 121568

For example:

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

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

Upvotes: 3

alko989
alko989

Reputation: 7908

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

substr('LT50420331984221PAC00_B7.tif', 10, 16)

Upvotes: 22

Related Questions