I am currently using this REGEX to get whole words with a prefix of PRE_


This works fine for most cases, but I would like to handle cases when a special character is part of the word, such as a period, comma or other special character . , ; - { } ( ) [ ]

For example, the words here:

PRE_complexword()a.;.is deleted
Somewords ahead PRE_sometext() ending in other words
Words with bracket [PRE_brackettext] are deleted
PRE_sampleword is spaced out so deleted

I would like only to find the first part of a delimited word. So I can delete or replace the word. So, in the case of replacing all PRE_ prefixed words in this scenario with "" as the text, I would get:

<DELETED>()a.;.is deleted
Somewords ahead <DELETED>() ending in other words
Words with bracket [<DELETED>] are deleted
<DELETED> is spaced out so deleted

I was trying different REGEX, but nothing really matches entirely correct throughout the samples. Something like the following doesn't work:


Any help here would be greatly appreciated.


Method 1

Instead of including all non-space character (\S in your first regex) after PRE_, simply state all "non-delimiters" in your search and it should do.

The following works for all of your example, by replacing \S with [A-Za-z]:


If you wish to include numeric, hyphen (-) and underscore (_) you can use the following instead:


Method 2

Otherwise, you could modify your \S into "anything but \s and other delimiters such as ., ,, ;, {. }, (, ), [, ] (and any others at your wish)" In that way your regex becomes


The phrase [^blahblah] means any character except blahblah.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.