How to remove only words that end with period with regex?

Member

by rollin , in category: Third Party Scripts , 24 days ago

How to remove only words that end with period with regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 23 days ago

@rollin 

To remove only words that end with a period using regex, you can use the following pattern:

1
w+.


Explanation of the pattern:

  •  : This is a word boundary that ensures that the pattern matches whole words only.
  • w+ : This matches one or more word characters (alphabetic, numeric, or underscore).
  • . : This matches a period at the end of the word.


Using this pattern, you can remove words that end with a period from a string by replacing them with an empty string. Here is an example in Python:

1
2
3
4
5
6
import re

text = "This is a sample sentence. Remove words like this. Keep other words intact."
new_text = re.sub(r'w+.', '', text)

print(new_text)


Output:

1
This is a sentence. Remove words like Keep other words intact.


This will remove only words that end with a period while keeping other words intact.