python正则表达式findall无法正确匹配

时间:2018-01-01 04:35:21

标签: python regex

    /// <summary>
    /// Query with dynamic Include
    /// </summary>
    /// <typeparam name="T">Entity</typeparam>
    /// <param name="context">dbContext</param>
    /// <param name="includeProperties">includeProperties with ; delimiters</param>
    /// <returns>Constructed query with include properties</returns>
    public static IQueryable<T> MyQueryWithDynamicInclude<T>(this DbContext context, string includeProperties)
       where T : class
    {            
        string[] includes = includeProperties.Split(';');
        var query = context.Set<T>().AsQueryable();

        foreach (string include in includes)
            query = query.Include(include);

        return query;
    }

我希望匹配&#39; [M] +&#39;和&#39; [M] 2 +&#39;
但结果是

re.findall("\[M(\+\d)?\](\d)?\+", u'[2][M]+[2][M]2+')

1 个答案:

答案 0 :(得分:-1)

您已在正则表达式模式中捕获了组,这使得findall返回组;如果您需要返回匹配项,则可以使用?:

将捕获的组转换为未捕获的组
import re
re.findall("\[M(?:\+\d)?\](?:\d)?\+", u'[2][M]+[2][M]2+')
#               ^^         ^^
# ['[M]+', '[M]2+']