mercredi 6 mai 2015

Matching multiple words within a string with regular expression

I am using Python to match a few words within a sentence and testing them against unit tests. I want a regular expression that matches all these words and gives me these outputs mentioned below:

firstword = "<p>This is @Timberlake</p>"
outputfirstword = "@Timberlake"

Finds the word that starts with the @ symbol

secondword = "<p>This is @timber.lake</p>"
outputsecondword = "@timber.lake"

Period between words are okay.

thirdword = "This is @Timberlake. Yo!"
outputthirdword = "@Timberlake"

If there is a space after the period then both the period and space don't count towards the outputthirdword

fourthword = "This is @Timberlake."
outputfourthword = "@Timberlake"

The final period (.) is not included.

Aucun commentaire:

Enregistrer un commentaire