您需要将字符串拆分成多个字段,但是分隔符在整个字符串中并不一致。
您可以通过多种方式在python中拆分一个或多个分隔符的字符串。最简单的方法是使用该split()
方法,但是,它旨在处理简单的情况。
re.split() is more flexible than the normal `split()` method in handling complex string scenarios.
使用re.split()可以为分隔符指定多个模式。如解决方案中所示,分隔符为ahyphen(-)或whitespace()或逗号(,)后跟的值。正则表达式文档可以在这里找到。
每当找到该模式时,整个匹配都将成为匹配两侧的字段之间的分隔符。
仅提取定界符之间的文本(不定界符)。
import re tennis_greats = 'Roger-federer, Rafael nadal, Novak Djokovic,Andy murray' """" #------------------------------------------------------------------------------------------- # Scenario 1 - Output the players # Input - String with multiple delimiters ( - , white space) # Code - Specify the delimters in [] #------------------------------------------------------------------------------------------- """ players = re.split(r'[-,\s]\s*',tennis_greats)
print(f" The output is - {players}")
输出为-[“ Roger”,“ federer”,“ Rafael”,“ nadal”,“ Novak”,“ Djokovic”,“ Andy”,“ Murray”]
提取定界符之间的文本以及定界符
import re tennis_greats = 'Roger-federer, Rafael nadal, Novak Djokovic,Andy murray' """" #------------------------------------------------------------------------------------------- # Scenario 2 - Output the players and the delimiters # Input - String with multiple delimiters ( - , white space) # Code - Specify the delimters between pipe (|) #------------------------------------------------------------------------------------------- """ players = re.split(r'(-|,|\s)\s*',tennis_greats)
print(f" The output is -{players}")
输出为-['Roger','-','federer',',','Rafael',','nadal',',','Novak',','Djokovic',',' ,“安迪”,“”,“墨累”]