美文网首页
正则表达式

正则表达式

作者: VinZZZZ | 来源:发表于2019-08-20 11:32 被阅读0次

    官方手册: https://docs.python.org/zh-cn/3.7/library/os.path.html

    验证网站:

    https://regexr.com/4je42 (比较清晰明了,但不支持re.MULTILINE 多行的验证)

    https://regex101.com (支持多行验证)

    过滤单行和多行注释

     def filter_useless_chars(content):
            # 移除单行注释和多行注释 (已加了防止如 str = "sfasfasdf//fafasdf"等的误删判断)
            def _remove_comments(string):
                pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*$)"
                regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
    
                def _replacer(match):
                    if match.group(2) is not None:
                        return ""
                    else:
                        return match.group(1)
    
                return regex.sub(_replacer, string)
    
            file_str = _remove_comments(content)
            return file_str
    

    匹配任意字符(不包括换行符).
    匹配任意字符(包括换行符): [\s\S]
    或者在Python中用如re.compile(pattern, re.DOTALL)re.DOTALL来标记,此时.可以匹配任意字符(包括换行符)。

    相关文章

      网友评论

          本文标题:正则表达式

          本文链接:https://www.haomeiwen.com/subject/eckusctx.html