regex - Regular expression for condition until next space -
how write regular expression grabs capital letter located anywhere subsequent character until space?
input: cake pietypeapple crumble tart toasttexas price
for example, want grab "apple" despite not being preceded space. want "crumble". want "texas" though not of components upper case.
i use gsub(pattern, replacement = "", x = string)
following output
output: cake pietype tart toast price
thanks!
you can use regmatches
extract these substrings.
> x <- 'cake pietypeapple crumble tart toasttexas price' > regmatches(x, gregexpr('[a-z]\\s+', x))[[1]] # [1] "apple" "crumble" "texas"
alternatively, if want strict on matching letter characters only.
> regmatches(x, gregexpr('[a-z][a-za-z]+', x))[[1]]
if want replace them, use following avoid excess space left in between words.
> gsub('[a-z][a-za-z]+( [a-z][a-za-z]+)*', '', x) # [1] "cake pietype tart toast price"
Comments
Post a Comment