检查两个字符串是否在python中包含相同的模式

时间:2016-08-05 08:34:35

标签: python regex string pattern-matching

我有以下列表:

names = ['s06_215','s06_235b','s06_235','s08_014','18:s08_014','s08_056','s08_169']

s06_235bs06_235s08_01418:s08_014重复。但是,如示例所示,命名中没有特定的模式。我需要对列表元素进行成对比较:

for i in range(0, len(names)-1):
    for index, value in enumerate(names):
        print names[i], names[index]

然后我需要检查每对,如果两个,包含相同的字符串,但长度超过4。这是s06_235bs06_235s08_01418:s08_014会通过此标准,但s08_056s08_169则不会。

我如何在Python中实现这一目标?

2 个答案:

答案 0 :(得分:2)

您可以使用某些不属于这些字符串的特殊字符迭代所有combinationsjoin,并使用(\w{5,}).*#.*\1之类s1 in s2来查找那对中重复的一组。除了仅使用(\w{5,})进行测试之外,如果第一个字符串的一部分包含在第二个字符串中,则此功能也可以使用,反之亦然。

这里,\w是至少5个字符的共享子字符串(在这种情况下来自.*类,但可以自由调整),后跟更多字符#分隔符(在这种情况下为.*),更多填充\1,然后是第一组p = re.compile(r"(\w{5,}).*#.*\1") for pair in itertools.combinations(names, 2): m = p.search("#".join(pair)) if m: print("%r shares %r" % (pair, m.group(1))) 的另一个实例。

('s06_215', 's06_235b') shares 's06_2'
('s06_215', 's06_235') shares 's06_2'
('s06_235b', 's06_235') shares 's06_235'
('s08_014', '18:s08_014') shares 's08_014'
('s08_014', 's08_056') shares 's08_0'
('18:s08_014', 's08_056') shares 's08_0'

输出:

_

当然,您可以调整正则表达式以满足您的需求。例如,如果您不希望重复区域被p = r"([a-z0-9]\w{3,}[a-z0-9]).*#.*\1"限制,则可以使用if @@ROWCOUNT > 0 return ;之类的正则表达式。

答案 1 :(得分:1)

您可以使用'in'运算符来查看变量是否包含另一个

  public class Product
{

    //private product data
    private string productName;

    public string getProductName()
    {
        return this.productName;
    }

    public void setProductName (string inProductName)
    {
        this.productName = inProductName;
    }

    private string customerName;

    public string getCustomerName()
    {
        return this.customerName;
    }

    public void setCustomerName (string inCustomerName)
    {
        this.customerName = inCustomerName;
    }

    private string firmwareLocation;

    public string getFirmwareLocation()
    {
        return this.firmwareLocation;
    }

    public void setFirmwareLocation (string inFirmwareLocation)
    {
        this.firmwareLocation = inFirmwareLocation;
    }


    //constructor 
    public Product (string inProductName, string inCustomerName, string inFirmwareLocation)
    {
        productName = inProductName;
        customerName = inCustomerName;
        firmwareLocation = inFirmwareLocation;
    }


    //save method
    public void Save (System.IO.TextWriter textOut)
    {
        textOut.WriteLine(productName);
        textOut.WriteLine(customerName);
        textOut.WriteLine(firmwareLocation);
    }

    public bool Save (string filename)
    {
        System.IO.TextWriter textOut = null;
        try
        {
            textOut = new System.IO.StreamWriter(filename);
            Save(textOut);
        }
        catch
        {
            return false;
        }
        finally
        {
            if (textOut != null)
            {
                textOut.Close();
            }
        }
        return true;
    }

试试这个:

private void Add_Click(object sender, RoutedEventArgs e)
    {
        //get input from user
        string inputCustomerName = customerNameTextBox.Text;
        string inputProductName = productNameTextBox.Text;
        string inputFirmwareLocation = firmwareTextBox.Text;

        try
        {
            Product newProduct = new Product(inputProductName, inputCustomerName, inputFirmwareLocation);
            newProduct.Save("products.txt");
            MessageBox.Show("Product added");
        }
        catch
        {
            MessageBox.Show("Product could not be added");
        }
    }

编辑: 正如tobias_k所提到的:请注意,这仅在整个字符串包含在另一个字符串

中时才有效
相关问题