Reputation: 53
I'm new to regular expressions. I'm trying to split a string in python when it encounters . or ! or ? or \n
re.split('\?|\.|\!|\n', input_string)
here is an example:
input_string = "hi I need help with re. I have 99.9% chance to get help here!"
output = ["hi I need help with re.", "I have 99.9% chance to get help here!"]
Upvotes: 1
Views: 509
Reputation: 785581
findall
instead of split
since you want matches before a set of characters.Regex:
\S.*?(?:[?!\n]|(?<!\d)\.(?!\d))
RegEx Details:
\S
: Match a non-whitespace character.*?
: Match 0 or more of any characters(?:
: Start non-capture group
[?!\n]
: Match one of these characters inside [...]
|
: OR(?<!\d)\.(?!\d)
: Match a dot if it not preceded and followed by a digit)
: End non-capture groupCode:
import re
input_string = "hi I need help with re. I have 99.9% chance to get help here!"
print ( re.findall('\S.*?(?:[?!\n]|(?<!\d)\.(?!\d))', input_string) )
Output:
['hi I need help with re.', 'I have 99.9% chance to get help here!']
Upvotes: 2