简体   繁体   中英

Regex Expression with special characters

I am new with regex expressions in python. I am working in a csv file in which I have extracted all the rows. 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.

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

OR

Use 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? 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:

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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