简体   繁体   English

Python中的正则表达式如何查找成对单词

[英]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代码和测试正则表达式:

REGEX WEBSITE REGEX网站

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

相关问题 如何在 python 中使用正则表达式查找某些单词? - How to find certain words using regular expression in python? 如何使用Python正则表达式查找所有出现字母的单词TWICE? - How to find all the words that have a letter appears TWICE in them with Python regular expression? 如何在 python 中编写正则表达式以查找所有两个辅音相邻的单词 - how to write regular expression in python to find all Words with two consonants next to each other Python 使用正则表达式,如何找到第二次出现以及在行中的单词之间 - Python using regular expression, how to find second occurrence and in between of words in line python正则表达式中的整个单词 - Whole words in python regular expression Python 正则表达式 - 查找不同行上两个特定单词之间的所有单词 - Python Regular Expression - Find all words between two specific words on different lines 如何在Python中的正则表达式中间省略单词? - How can I omit words in the middle of a regular expression in Python? Python:如何使用正则表达式仅匹配一个单词 - Python : How to match only one of the words using regular expression 在正则表达式中查找正则表达式(Python 3.5) - Find regular expression in regular expression (Python 3.5) 在字符串中查找一系列大写单词的正则表达式 - Regular expression to find a series of uppercase words in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM