Trying to find a solution to replace all occurance of fullstop in a sentence except if its an email.
e.g
Input:
Hello, Thanks for reaching us, we really appreciate your feedback! We are always trying to improve our product to provide you with a better experience.I will be sure to pass your suggestion onto our team for their consideration as we move forward with this. Call XYZ on 12345 or email at [email protected] if you have any question. Thank you for your email.
expected output:
Hello, Thanks for reaching us, we really appreciate your feedback! We are always trying to improve our product to provide you with a better experience.
I will be sure to pass your suggestion onto our team for their consideration as we move forward with this.
Call XYZ on 12345 or email at [email protected] if you have any question.
Thank you for your email
2
Answers
I hope this help you :
First i just found email address in text and define that’s position
Then i remove email from text and replace all dot (.) with double end of line
And at last i again add email to text at the correct position
Split the text based on spaces.
Consider each continuous string of characters as tokens.
For each token, check if we have a period character
.
and if we have, check if it’s an email usingfilter_var
. The regex approach to check if a string is an email is too complicated. Seethis
.If it’s not an email, replace the period character with a period character and new lines.
Implode the array back to a string based on a single space. Your text is now formatted with new lines.
Snippet:
Live Demo