简体   繁体   中英

How to use regular expression to retrieve data in python?

I have a string defined as,

content = "f(1, 4, 'red', '/color/down1.html');    
f(2, 5, 'green', '/color/colorpanel/down2.html');    
f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"

Here is the code I tried but it doesn't work:

results = re.findall(r"f(.*?)", content)
for each in results:
    print each

How to use regular expression to retrieve the links within the content? Thanks.

You can learn the basic regexes on https://regex101.com/ and http://regexr.com/

In [4]: import re

In [5]: content = "f(1, 4, 'red', '/color/down1.html');    \
   ...: f(2, 5, 'green', '/color/colorpanel/down2.html');   \
   ...: f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"

In [6]: p = re.compile(r'(?=/).*?(?<=.html)')

In [7]: p.findall(content)
Out[7]: 
['/color/down1.html',
 '/color/colorpanel/down2.html',
 '/color/colorpanel/colorlibrary/down3.html']

.*? matches any character (except for line

*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)

You can also just get the last /

In [8]: p2 = re.compile(r'[^/]*.html')

In [9]: p2.findall(content)
Out[9]: ['down1.html', 'down2.html', 'down3.html']

[^/]* Match a single character not present in the list below

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

/ matches the character / literally (case sensitive)

. matches any character (except for line terminators) html matches the characters html literally (case sensitive).

Or, you can extract all the data in f()

In [15]: p3 = re.compile(r"(?=f\().*?(?<=\);)")

In [16]: p3.findall(content)
Out[16]: 
["f(1, 4, 'red', '/color/down1.html');",
 "f(2, 5, 'green', '/color/colorpanel/down2.html');",
 "f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"]

你可以这样做:

re.findall(r"f\(.*,.*,.*, '(.*)'", content)

You can try like so:

import re

content = """f(1, 4, 'red', '/color/down1.html');    
    f(2, 5, 'green', '/color/colorpanel/down2.html');    
    f(3, 6, 'blue', '/color/colorpanel/colorlibrary/down3.html');"""

print re.findall(r"(\/[^']+?)'", content)

Output:

['/color/down1.html', '/color/colorpanel/down2.html', '/color/colorpanel/colorlibrary/down3.html']  

Regex:

(\\/[^']+?)' - match / followed by 1 or more non ' characters till first occurence of ' and capture in group1.

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