2

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

\b(PRE_)\S*

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_samplewordwithoutdelimiter
PRE_sampleword.otherwordsnotincluded;
PRE_Sampleword{}...deleted
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
sampleword.PRE_deleted;
notdeleted.notdeleted.PRE_

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>
<DELETED>.otherwordsnotincluded;
<DELETED>{}...deleted
<DELETED>()a.;.is deleted
Somewords ahead <DELETED>() ending in other words
Words with bracket [<DELETED>] are deleted
<DELETED> is spaced out so deleted
sampleword.<DELETED>;
notdeleted.notdeleted.<DELETED>

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

\b(PRE_)\S*(?:[;]|[.][-])$

Any help here would be greatly appreciated.

1

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]:

\bPRE_[A-Za-z]*

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

\bPRE_[-A-Za-z0-9_]*

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

\bPRE_[^.;,{}()[\]\s]*

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.