Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex to split on certain patterns with skip patterns

Tags:

python

regex

I want to split a Python string on certain patterns but not others. For example, I have the string

Joe, Dave, Professional, Ph.D. and Someone else

I want to split on \sand\s and ,, but not , Ph.D.

How can this be accomplished in Python regex?

like image 244
chriskirk Avatar asked Jul 29 '11 03:07

chriskirk


People also ask

How do you split a string by the occurrences of a regex pattern Python?

Introduction to the Python regex split() function The built-in re module provides you with the split() function that splits a string by the matches of a regular expression. In this syntax: pattern is a regular expression whose matches will be used as separators for splitting. string is an input string to split.

Can I use regex with split Python?

Regex to Split string with multiple delimitersWith the regex split() method, you will get more flexibility. You can specify a pattern for the delimiters where you can specify multiple delimiters, while with the string's split() method, you could have used only a fixed character or set of characters to split a string.

How do you split a string with special characters in Python?

Use the re. split() method to split a string on all special characters. The re. split() method takes a pattern and a string and splits the string on each occurrence of the pattern.


1 Answers

You can use:

re.split(r'\s+and\s+|,(?!\s*Ph\.D\.)\s*', 'Joe, Dave, Professional, Ph.D. and Someone else')

Result:

['Joe', 'Dave', 'Professional, Ph.D.', 'Someone else']
like image 164
Qtax Avatar answered Nov 04 '22 22:11

Qtax