Rawrshark
Rawrshark

Reputation: 109

How to delete words containing a specific character in RegEx?

Using Notepad++'s find and replace feature using regular expressions, I want to get rid of every word with the number symbol (#) attached to it, particularly in the beginning of the word in my case.

For example, how do I make:

The #7kfe dog likes #9kea to eat pizza

into:

The dog likes to eat pizza

Any help would be greatly appreciated. Thank you.

Upvotes: 2

Views: 4111

Answers (4)

Brian Stephens
Brian Stephens

Reputation: 5271

find: (\W)#\w+

replace: \1

(obviously also set it to regex mode)

The \W looks for a non-word character, to ensure the # is at the beginning of a word. The \1 in the replace puts that character back.

Upvotes: 1

h2ooooooo
h2ooooooo

Reputation: 39542

Most other responses will give you words starting with # which seems to be what you want, but to suit your question in your OP ("particularly in the beginning"), this will select every word with a # in it (anywhere):

/(\w*#\w+|\w+#\w*)/

DEMO

Upvotes: 3

dcsohl
dcsohl

Reputation: 7406

Most editors that have find-and-replace using regular expressions work similarly... in the 'find' field, look for #\w* and in the replace field, use (empty string). This will leave double spaces (the space that was before your word and the space that was after your word)... you can either tweak the expression above to something like #\w* ? (so that the space is optional, in case the word in question is the last word of the line), or you can do a second search-and-replace that collapses multiple spaces into one.

Upvotes: 3

X-Pippes
X-Pippes

Reputation: 1170

#\w*

use this regex.

will match every word after #

Upvotes: 0

Related Questions