Jump to content

Python Regular Expression Help.

zarrexx
Go to solution Solved by dannytech357,

This may be a little closer to what you want:

(M(?:r|rs|s)(?:\.?) (?:[A-Za-z\s]*?))(?:\s[a-z]|\n|$|\.)

It will also match Mr, Mrs, and Ms and read until it finds a space followed by a lowercase character, the end of the line, or the end of the string. It will match everything the way you wanted it to. I also added (?:\.?) to optionally match Mr., Mrs., and Ms., but you can remove that if you don't think you need that

To solve this problem I must use regular expressions, the example is given below. If the name starts with a "Mr", "Mrs", or "Ms" then all the proceeding capital words have to be included. Does anyone know the code for that? Thanks!

 

sample input:

Mr George Martin wrote Game of Thrones. The Lord of the Rings was written by Mr JRR Tolkien. Ms Enid Blyton wrote The Famous Five. Mr AA Milne wrote Winne-the-Pooh. Mr Robin Williams played Mrs Doubtfire. Mr Fred Martin Albert Finnegan did not write anything.

sample output:

Mr George Martin

Mr JRR Tolkien

Ms Enid Blyton

Mr AA Milne

Mr Robin Williams

Mrs Doubtfire

Mr Fred Martin Albert Finnegan

Link to comment
Share on other sites

Link to post
Share on other sites

I only got this

^M(r|rs|s) ([A-Z].*)+$

It matches anything that starts with Mr, Mrs or Ms then a space, then a capital letter followed by 0 to unlimited other characters. That's where I got stuck. I don't know how to break out of that infinite character from the period when a space is the next character and match following words starting with a capital.

Link to comment
Share on other sites

Link to post
Share on other sites

This may be a little closer to what you want:

(M(?:r|rs|s)(?:\.?) (?:[A-Za-z\s]*?))(?:\s[a-z]|\n|$|\.)

It will also match Mr, Mrs, and Ms and read until it finds a space followed by a lowercase character, the end of the line, or the end of the string. It will match everything the way you wanted it to. I also added (?:\.?) to optionally match Mr., Mrs., and Ms., but you can remove that if you don't think you need that

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×