4

I'm trying to remove words that feature a "com" and ends with a ".". I would like to remove the full word and leave the "."

Here is an example of the starting text:

This is a test testcom.

I would like the output to be:

This is a test .

What I have come up with so far is:

(?<=\ ).*?com(?=\.)

but this seems to highlight the full sentence to the first space.

bakingsoda
  • 57
  • 7

2 Answers2

1

The (?<=\ ).*?com(?=\.) pattern matches a location after a space, then matches any 0 or more chars other than line break chars but as few as possible up to the first occurrence of com followed with a dot. So, it will match any amount of spaces on its way from the matched location after space to the com. substring.

If com can be anywhere inside the word you may use either of the two expressions below:

[^\s.]*com[^\s.]*\.
\w*com\w*\.

and replace with a ..

Or, equivalent:

[^\s.]*com[^\s.]*(?=\.)
\w*com\w*(?=\.)

and replace with an empty string.

If the com must be at the end of the word, just remove the second [^\s.]* / \w* from the above expressions.

Details

  • [^\s.]*com[^\s.]*\. - matches 0+ chars other than whitespace and ., then com and then again 0+ chars other than whitespace and . and a .
  • \w*com\w*\. - matches 0+ letters/digits/_, then com, then again 0+ letters/digits/_ and then ..

Notepad++ demo and settings:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

I believe this one will suit you:

\w*?com(?=\.)
zipa
  • 27,316
  • 6
  • 40
  • 58