简体   繁体   中英

Python re I want to split at all punctuation except for the - symbol

re.split("\\W+", words)

I have the code above which splits the string at all punctuation but how do I split it at all punctuation except for the '-' symbol?

Use re.split(r'[^-\w]+', words)

Where ^ inside the character group indicates not to split on ( - or any alphabet ) characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM