0
点赞
收藏
分享

微信扫一扫

[正则] - 学习过程1

杰森wang 2022-01-22 阅读 179
pythonregex

1. 判断是否以xxxx开头: 以数字. 开头,如“2. ”

    if re.match('^\d+\. ', content):
        return '<h3>%s</h3>' %(content)

2. 将内容中以[ dsfda789 df    ] 的内容替换成tpl

index = 0
input = {
    'w': 60         # input宽度
}


def replaceValue(matched):
    global index, input
    index = index + 1
    tpl = '<input type="text" id="Text%d" class="chgClss" value="%s" style="width:%dpx; border-bottom-color:#ff0000; border-left:0px; border-right:0px; border-top:0px; font-size:18px; color:#ff0000; text-align:center;" />'

    value = matched.group('value')

    return tpl %(index, value.strip('[] '), input['w'])


def myReplace(content):
    pattern = '(\[\s*[\w\/]+\s*\])'
    content = re.sub('(?P<value>%s)' %(pattern), replaceValue, content)
    # print(content)
    return '<p>%s</p>' %(content)
举报

相关推荐

0 条评论