我有一个脚本搜索,比如说,一个字+“(”我会用:Python3:输入命令行正则表达式
pattern = re.compile("anyword\(")
但我希望让用户从进入模式字符串命令行 (视窗10)。
argp = argparse.ArgumentParser()
argp.add_argument("-p", metavar="pattern", type=str,
, help="regex pattern")
args= argp.parse_args()
pattern = re.compile(args.p)
但是未办理anyword\(
正常。反正有做到这一点?
我试过以下,但没有得到我吨至re.compile(anyword\(
):
pattern = re.compile(bytes(args.p, "utf-8").decode('unicode_escape'))
pattern = re.compile(str.encode(args.p).decode('unicode_escape'))
pattern = re.compile(eval(args.p))
你能分享输入字符串吗? –
用户在命令行上输入时可能需要转义反斜杠。 – Barmar