如何读取字符串之间的每个字符?

2024-07-08 15:59:13 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在使用以下代码查找python中的字体颜色: 样式函数包含所有html标记

if style.find("color: #3C3C3C")>=0:
    use_raw = '%s%s' % (use_raw, 'color: #FF0000;')

但在这里我不想让如果条件与颜色具体。我想为所有的颜色做些什么


Tags: 函数代码标记rawifstyle颜色use
1条回答
网友
1楼 · 发布于 2024-07-08 15:59:13

可以使用正则表达式匹配要查找的字符串类型:

import re

def FindColor(styleString):
    colourRegex = re.compile(r'(color: #)([a-fA-F0-9]{6})')
    matchObject = colorRegex.search(styleString)
    colorValue = matchObject.group(2)       # Isolates the 6-character hex color value
    # Do any extra processing here, based on *colorValue*, and create your *use_raw* value.  For example:
    use_raw = matchObject.group(1) + 'FF0000;'
    return use_raw

以指定的格式返回任何输入颜色的以下值:

color: #FF0000;

相关问题 更多 >

    热门问题