简体   繁体   中英

consecutive punctuation and alpha-numeric characters

Probably a simple question, but I don't have a lot of regex experience. I would like to take a string and select all the consecutive punctuation characters and all the consecutive alpha-numeric characters

This is as close as I could get

r="my9zza :)asax"
import re
re.findall(r'(\w+)|([^a-zA-Z0-9\s]+)', r)

returns

[('my9zza', ''), ('', ':)'), ('asax', '')]

but I would like

['my9zza', ':)', 'asax']

Simply use:

r = "my9zza :)asax"
import re
print(re.findall(r'\w+|[^a-zA-Z0-9\s]+', r))

The problem was having two sets of parentheses in your original code, causing findall to return a 2-ple.

If you wanted to keep the original regex, you can also easily transform your result into the desired output with:

[x[0] or x[1] for x in result]

You can try this:

s = [('my9zza', ''), ('', ':)'), ('asax', '')]
final_s = [[b for b in i if b][0] for i in s]

Output:

['my9zza', ':)', 'asax']

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