具有特殊字符的正则表达式

时间:2015-07-10 06:45:23

标签: python regex string

我是python中正则表达式的新手。 我正在一个csv文件中,我已经提取了所有行。 现在我无法进行搜索。我希望找到这个[[*]],*,因为它们之间没有任何字符。任何帮助 目前我在循环中使用它

    searchObj = re.match( r'\[\[(.*)\]\]', str(row), re.M|re.I)

3 个答案:

答案 0 :(得分:1)

使用$resultArray = uniqueAssocArray($actualArray, 'RecipientID'); function uniqueAssocArray($array, $uniqueKey) { if (!is_array($array)) { return array(); } $uniqueKeys = array(); foreach ($array as $key => $item) { $groupBy=$item[$uniqueKey]; if (isset( $uniqueKeys[$groupBy])) { //compare $item with $uniqueKeys[$groupBy] and decide if you //want to use the new item $replace= false; } else { $replace=true; } if ($replace) $uniqueKeys[$groupBy] = $item; } return $uniqueKeys; 提取这些括号内的文本,因为re.findall尝试从字符串的开头匹配。

re.match

使用find_lst = re.findall( r'\[\[(.*?)\]\]', str(row), re.M|re.I)

re.search

答案 1 :(得分:0)

searchObj = re.match( r'.*?\[\[(.*?)\]\]', str(row), re.M|re.I).groups()

match从头开始匹配。所以你可以从头开始比赛并抓住小组。

答案 2 :(得分:0)

如上所述,findall解决方案应该可以正常工作吗?你需要给出一个示例行,然后我们就可以看出为什么它不适合你。假设您已将CSV行读入列列表中,则应该看到以下内容:

row = ["Col1", "Col2 has [[foo]] in it", "Col3", "Col4 has [[foo]]] and [[bar]]"]

for col in row:
    print re.findall(r'\[\[(.*?)\]\]', col, re.M|re.I)

,并提供:

[]
['foo']
[]
['foo', 'bar']