Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use regular expressions in python to split articles based on punctuation

I need to divide the article into sentences by punctuation. I use the following regular expression:

re.split(r'[,|.|?|!]', strContent)

It does work, but there is a problem. It will separate the following Latin names that should not be split (such as G. lucidum):

Many studies to date have described the anticancer properties of G. lucidum,

The abbreviation of this Latin name is a capital letter followed by a dot and a space.
So I try to modify the above regular expression as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

re.split(r'[,|(?:[^A-Z].)|?|!]', strContent)

However, the following error prompt was received:

re.error: unbalanced parenthesis

How can I modify this regular expression?

>Solution :

You should use a negative lookbehind, and put it before the character set that matches the sentence ending.

The negative lookbehind should match a word that’s just a single capital letter. This can be done by matching a word boundary before the letter with \b.

You also don’t need | inside the character set. That’s used for alternative patterns to match.

re.split(r'(?<!\b[A-Z])[,.?!]', strContent)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading