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

Member

by rollin , in category: Third Party Scripts , 5 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 5 months 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.

Related Threads:

How to remove end of substring using regex?
How to replace spaces between words using regex?
How to match complete words for acronym using regex?
How to match a sentence between 2 words using regex?
How to match all words after a specific expressions with regex?
How to remove . (dot) from email before @ using regex?