regex - Extracting number from text string referencing specific text -
this question has answer here:
i have dataframe following:
columna=c("kuala lumpur sector 2 new","old jakarta sector31", "sector 9, 7 hong kong","jakarta new sector22") df1 <- data.frame(columna)
from extract sector
in instances, i.e.:
2,31,9,22
in cases number preceded word 'sector'. however, there may or may not space before number. although not in example above, there may other irrelevant numbers in text string, want ignore. numbers range 1-30, no 100s or above involved.
i'm afraid regular expression experience nil. appreciated. also, future use, if there regex guides specific r, appreciate heads-up.
for example using gsub
, grouping:
gsub(".*sector ?([0-9]+).*","\\1",columna) [1] "2" "31" "9" "22"
Comments
Post a Comment