Python REGEX搜索返回None作为答案

时间:2017-01-30 11:37:57

标签: python regex python-2.7 python-3.x

我得到的是这个代码的答案。当我单独发送电子邮件或电话时,代码可以正常工作。但是使用两者都返回none。请帮忙!

导入重新

string = 'Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'
contacts = re.search(r'''
    ^(?P<email>[-\w\d.+]+@[-\w\d.]+) # Email
    (?P<phone>\d{3}-\d{3}-\d{4})$ # Phone
''', string, re.X|re.M)

print(contacts.groupdict)

2 个答案:

答案 0 :(得分:1)

也许你想要:

    private bool _columnWidthChanging;
    private void ColumnWidthPropertyChanged(object sender, EventArgs e)
    {
        // listen for when the mouse is released
        _columnWidthChanging = true;
        if (sender != null)
            Mouse.AddPreviewMouseUpHandler(this, BaseDataGrid_MouseLeftButtonUp);
    }

    void BaseDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (_columnWidthChanging)
        {
            _columnWidthChanging = false;
            var dgCs = FindVisualChildByName<DataGrid>(Test, "dgC");
            var dgH = FindVisualChildByName<DataGrid>(Test, "dgH").First();
            foreach (var dgC in dgCs)
            {
                for (int i = 0; i < dgC.Columns.Count; i++)
                {

                    var column = dgH.Columns[i];
                    dgC.Columns[i].Width = column.ActualWidth;
                }
            }
        }
    }

    private void Grid_Loaded(object sender, RoutedEventArgs e)
    {
        var dgC = FindVisualChildByName<DataGrid>(Test, "dgC").First();
        var dgH = FindVisualChildByName<DataGrid>(Test, "dgH").First();
        for (int i = 0; i < dgC.Columns.Count; i++)
        {
            var column = dgC.Columns[i];
            dgH.Columns[i].Width = column.ActualWidth;

            PropertyDescriptor pd = DependencyPropertyDescriptor
                         .FromProperty(DataGridColumn.ActualWidthProperty,
                                       typeof(DataGridColumn));

                //Add a listener for this column's width
                pd.AddValueChanged(dgH.Columns[i],
                                   new EventHandler(ColumnWidthPropertyChanged));


        }

这匹配部分:

(?P<email>[-\w\d.+]+@[-\w\d.]+), (?P<phone>\d{3}-\d{3}-\d{4})

Debuggex Demo

答案 1 :(得分:0)

您正在使用^$对整个字符串强制执行匹配。您的正则表达式似乎只与子字符串匹配。