[英]Regular Expression in python how to find paired words
I'm doing the cipher for python. 我正在为python加密。 I'm confused on how to use Regular Expression to find a paired word in a text dictionary.
我对如何使用正则表达式在文本字典中查找成对的单词感到困惑。
For example, there is dictionary.txt with many English words in it. 例如,其中包含许多英语单词的dictionary.txt。 I need to find word paired with "th" at the beginning.
我需要在开头找到与“ th”配对的单词。 Like they, them, the, their .....
就像他们,他们,他们的.....
What kind of Regular Expression should I use to find "th" at the beginning? 一开始我应该使用哪种正则表达式来查找“ th”?
Thank you! 谢谢!
如果您有一个单词列表(因此每个单词都是一个字符串),那么您会找到以“ th”开头的单词:
yourRegEx = re.compile(r'^th\w*') # ^ for beginning of string
^(th\\w*)
^(th \\ w *)
gives you all results where the string begins with th . 给您所有以th开头的结果。 If there is more than one word in the string you will only get the first.
如果字符串中有多个单词,您只会得到第一个。
(^|\\s)(th\\w*)
(^ | \\ s)(th \\ w *)
wil give you all the words begining with th even if there is more than one word begining with th 即使有多个以th开头的单词,也会为您提供以th开头的所有单词
(th)\w*
notice you have this great online tool to generate python code and test regex: 请注意,您拥有这个强大的在线工具来生成python代码和测试正则表达式:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.