简体   繁体   English

从 Python 中的字符串列表确定一个常见的正则表达式模式

[英]Determine a common regex pattern from a list of strings in Python

I have a list of different strings.我有一个不同字符串的列表。 I would like to find out a common regex expression (if any) for that list using Python.我想使用 Python 为该列表找出一个常见的正则表达式(如果有)。
For example, my list contains the following strings:例如,我的列表包含以下字符串:

myList = ['This is first string', 'This is 2nd String', 'This is string 3', 'This is a final string']

Then I would like to programmatically determine if there is a regex expression which will match all the strings in the list.然后我想以编程方式确定是否存在匹配列表中所有字符串的正则表达式。 For instance, here the expression is something like 'This is *'.例如,这里的表达式类似于 'This is *'。 Will it be possible to do this programmatically?是否可以以编程方式执行此操作?

use operator |使用运算符| to join together each literal match.将每个文字匹配连接在一起。 place inside ^(...)$放在^(...)$里面

pattern = "^(" + "|".join(re.escape(item) for item in myStrings) + ")$"

它很简单, /\\'This is ([a-zA-Z0-9])*\\'/ /This is ([a-zA-Z0-9])*/不需要引号/\\'This is ([a-zA-Z0-9])+\\'/ - 任何东西都必须在 This is 之后。

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

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