简体   繁体   English

正则表达式

[英]regular expression

i am looking to find some words in a string of code i've already done for my class.我希望在我已经为我的 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;这给了我 44 个结果; 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)? \b匹配单词边界(单词的开头或结尾)和(tic|s)? optionally matches the string "tic" or "s".可选地匹配字符串“tic”或“s”。

Try r"gene(?:tic)?"试试r"gene(?:tic)?"
the tic is optional at the end. tic 最后是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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