Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何正确测试模式中的有效字符,可能使用字符数组或其他方法(regex)_Python_Regex_Arrays_Char_Character Arrays - Fatal编程技术网

Python 如何正确测试模式中的有效字符,可能使用字符数组或其他方法(regex)

Python 如何正确测试模式中的有效字符,可能使用字符数组或其他方法(regex),python,regex,arrays,char,character-arrays,Python,Regex,Arrays,Char,Character Arrays,我有一个在Python中似乎无法解决的问题。我有一个模式需要匹配字符数组中的任何字符。如果没有,那就有问题了。下面是我的例子: 模式="0000 006e 0022 0002 0156 00ac 0016 0016 0016 0016 0041 0016 0041 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 00

我有一个在Python中似乎无法解决的问题。我有一个模式需要匹配字符数组中的任何字符。如果没有,那就有问题了。下面是我的例子:

模式="0000 006e 0022 0002 0156 00ac 0016 0016 0016 0016 0041 0016 0041 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 001410016 05e0 0156 0055 0016 0e41“

allowedCharacters=“123456789abcdef”

因此,我的目标是查看模式是否符合允许的字符

这是我目前的代码

        # Test that there are only valid characters in the pattern.
        charPattern = list(pattern)
        expression = list("01234567890abcdef")

        for currentChar in pattern:
            if len(charPattern) - pattern[-1::-1].index("0123456789abcdef") - 1:
                self.assertTrue(False, logger.failed("The invalid character: " + currentChar + " was found in the IRCode pattern for: " + ircode))`enter code here`
欢迎提出任何想法/建议/代码示例

谢谢。

请尝试以下代码:

import re
p = re.compile(r'^[0123456789abcdef\s]+$')
str = "0000 006e 0022 0002 0156 00ac 0016 0016 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0041 0016 05e0 0156 0055 0016 0e41"

if(p.match(str)):
  print("passed")
else:
  list = re.findall('([^0123456789abcdef\s])+', str)
  print(list)
它将在字符串中搜索出现的
0123456789abcdef\s
字符。如果某个字符不在此模式中,则不会通过

编辑:

增加代码以防无法通过,将打印所有出现的无效的

尝试此代码:

import re
p = re.compile(r'^[0123456789abcdef\s]+$')
str = "0000 006e 0022 0002 0156 00ac 0016 0016 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0016 0016 0016 0016 0016 0016 0041 0016 05e0 0156 0055 0016 0e41"

if(p.match(str)):
  print("passed")
else:
  list = re.findall('([^0123456789abcdef\s])+', str)
  print(list)
def check (pattern, allowed):
    return not set(pattern) - set(allowed)
它将在字符串中搜索出现的
0123456789abcdef\s
字符。如果某个字符不在此模式中,则不会通过

编辑:


增加代码以防它无法通过,将打印所有出现的无效的

我感谢您的响应。您的示例非常有用!但是,如果模式中有无效字符(str)这与表达式不匹配,我怎么知道那个字符是什么?太好了!谢谢你的帮助。它工作得很好。还有一个问题。我怎么能使用相同的代码或新的正则表达式代码来查找空格、制表符、回车符和换行符?我们可以使用相同的p=re.compile示例(r'^[0123456789abcdef\s]+$)但是删除当前表达式并允许它查找空格、制表符、回车符和换行符。我最后尝试了这个方法,效果很好:重新编译(r'^[\t\r\n\s]+$),您只能使用
\s
(这意味着空白)。白景可以是:空格、制表符、回车符、换行符、垂直制表符或换行符。我很感激您的回答。您的示例非常有用!但是,如果模式中有无效字符(str)这与表达式不匹配,我怎么知道那个字符是什么?太好了!谢谢你的帮助。它工作得很好。还有一个问题。我怎么能使用相同的代码或新的正则表达式代码来查找空格、制表符、回车符和换行符?我们可以使用相同的p=re.compile示例(r'^[0123456789abcdef\s]+$)但是删除当前表达式并允许它查找空格、制表符、回车符和换行符。我最后尝试了这个方法,效果很好:重新编译(r'^[\t\r\n\s]+$),您只能使用
\s
(这意味着空白)。空白可以是:空格、制表符、回车符、换行符、垂直制表符或换行符。
def check (pattern, allowed):
    return not set(pattern) - set(allowed)