python 正则表达式解析链接:[http://example.com](一些文本示例)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 正则表达式解析链接:[http://example.com](一些文本示例)相关的知识,希望对你有一定的参考价值。

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"((?<=\[).*(?=\])|(?<=\().*(?=\)))"

test_str = "Visita il nostro [http://link.it](testo (senz)a tonde) e altri racconti dell'anno"

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

以上是关于python 正则表达式解析链接:[http://example.com](一些文本示例)的主要内容,如果未能解决你的问题,请参考以下文章

python爬虫之解析库正则表达式

Python 使用正则表达式解析日志

Python从零开始写爬虫-2 使用正则表达式解析HTML

Python:使用JSON API链接通过正则表达式显示命名捕获组

用于解析网页链接的正则表达式?

html文本中链接的正则表达式