简体   繁体   English

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

[英]Regex Expression with special characters

I am new with regex expressions in python. 我是python中的正则表达式的新手。 I am working in a csv file in which I have extracted all the rows. 我正在一个csv文件中提取所有行。 Now I can't do the searching. 现在我无法搜索。 I want to find this [[*]], * for any no of characters in between. 我想找到这[[*]],*表示之间没有任何字符。 Any help with it Currently I am using this in loop 任何帮助,目前我正在循环使用

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

Use re.findall to extract the text presnt inside those brackets, since re.match tries to match from the begining of a string. 使用re.findall提取这些括号内的文本re.match ,因为re.match尝试从字符串的re.match开始进行匹配。

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

OR 要么

Use re.search 使用re.search

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

match从头开始匹配。因此您可以从头开始匹配并抓住组。

As mentioned, the findall solution should work fine? 如前所述, findall解决方案应该可以正常工作吗? You need to give an example row, and then we can see why it is not working for you. 您需要提供示例行,然后我们可以了解为什么它对您不起作用。 Assuming you have read your CSV row into a list of columns then the following should be seen: 假设您已将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)

Giving: 给予:

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

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

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