Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Regex to remove digits except for words starting with #

I have some strings that can contain letters, numbers and '#' symbol.

I would like to remove digits except for the words that start with '#'

Here is an example:

"table9 dolv5e #10n #dec10 #nov8e 23 hello"

And the expected output is:

"table dolve #10n #dec10 #nov8e  hello"

How can I do this with regex, stringr or gsub?

like image 236
castaa95 Avatar asked Dec 07 '19 10:12

castaa95


1 Answers

How about capturing the wanted and replacing the unwanted with empty (non captured).

gsub("(#\\S+)|\\d+","\\1",x)

See demo at regex101 or R demo at tio.run (I have no experience with R)

My Answer is assuming, that there is always whitespace between #foo bar #baz2. If you have something like #foo1,bar2:#baz3 4, use \w (word character) instead of \S (non whitespace).

like image 53
bobble bubble Avatar answered Sep 19 '22 12:09

bobble bubble