简体   繁体   中英

How to match whole word in a string

I would like to pickup whole words in a string that separated by space, comma or period.

text = 'OTC GLUCOSAM-CHOND-MSM1-C-MANG-BOR test, dosage uncertain'
p = r"(?i)\b([A-Za-z]+[\s*|\,|\.]+)\b"    
for m in regex.finditer(p, str(text)):
    print (m.group())

I expect to get:
OTC
GLUCOSAM-CHOND-MSM1-C-MANG-BOR
test
dosage
uncertain

but what I got:
OTC
BOR
test,
dosage

To get a list of the words that you want, you can use the findall() function of the re module. Also, try changing the regular expressions to the one showed below:

text = 'OTC GLUCOSAM-CHOND-MSM1-C-MANG-BOR test, dosage uncertain'
result = re.findall('[\w]+[-?[\w]+]*', text)

print(result)
# outputs: ['OTC', 'GLUCOSAM-CHOND-MSM1-C-MANG-BOR', 'test', 'dosage', 'uncertain']

import re

text = 'OTC GLUCOSAM-CHOND-MSM1-C-MANG-BOR test, dosage uncertain'

p = r"[a-zA-Z-\d]*"

for m in re.finditer(p, str(text)):

if len(m.group().strip()) > 0:
  print(m.group())

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