简体   繁体   中英

regular expression

i am looking to find some words in a string of code i've already done for my class. i want to find if the abstracts of the literature i found includes the words "gene" or "genetic". so far i have

match = re.search(r"(gene|tic)"

which gives me 44 results; however, this expression is pulling anything that has the words gene or genetic in them (like general or biotic). how can i change this to only pull either gene or genetic, and nothing else? this must be a regular expression.

The following regex should match only on the exact 3 words "gene", "genes" and "genetic"

re.search(r"(\bgene(tic|s)?\b)")

\b matches word boundaries (the beginning or end of a word) and (tic|s)? optionally matches the string "tic" or "s".

Try r"gene(?:tic)?"
the tic is optional at the end.

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