Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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字符串中的字符[需要避免十六进制编码]_Python_Regex_Forward - Fatal编程技术网

“逃避”\&引用;python字符串中的字符[需要避免十六进制编码]

“逃避”\&引用;python字符串中的字符[需要避免十六进制编码],python,regex,forward,Python,Regex,Forward,需要从该字符串中选择IP地址 str1='' 当执行以下选项时 1. reg=re.compile(“^.*\/+([\d\.]+)/\+.*$”,re.I).搜索 mth=reg(str2) mth.组(1) 收到错误消息了吗 Traceback (most recent call last): File "<pyshell#90>", line 1, in <module> mth.group(1) AttributeError: 'NoneType'

需要从该字符串中选择IP地址
str1=''

当执行以下选项时 1.

reg=re.compile(“^.*\/+([\d\.]+)/\+.*$”,re.I).搜索
mth=reg(str2)
mth.组(1)

收到错误消息了吗

Traceback (most recent call last):
  File "<pyshell#90>", line 1, in <module>
    mth.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

您应该使用原始字符串形式:

str1 = r"<\11.1.1.1\cisco>"
print re.search(r'\b\d+(?:\.\d+)+\b', str1).group()
11.1.1.1
str1=r“”
打印重新搜索(r'\b\d+(?:\.\d+)+\b',str1.group()
11.1.1.1

您尝试了原始字符串。。。但是只在str1上,而不在正则表达式上?
error message - 
Traceback (most recent call last):
  File "<pyshell#90>", line 1, in <module>
    mth.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
str1 = r"<\11.1.1.1\cisco>"
print re.search(r'\b\d+(?:\.\d+)+\b', str1).group()
11.1.1.1
str1 = r'<\11.1.1.1\testdata>'
reg = re.compile(r"^.*?\\([\d\.]+)\\.*$",re.I)
mth = reg.search(str1)

print mth.group(1)
reg = re.compile("^.*?\\\([\d\.]+)\\\.*$",re.I)