我想匹配(在python中使用正则表达式):
http://images.mymaterials.com/images/steel-images/small/steel/steel800/steel800-2.jpg
Run Code Online (Sandbox Code Playgroud)
在以下字符串中:
http://www.mymaterialssite.com','http://images.mymaterials.com/images/steel-images/small/steel/steel800/steel800-2.jpg','Model Photo'
Run Code Online (Sandbox Code Playgroud)
我的代码有这样的东西:
temp="http://www.mymaterialssite.com','http://images.mymaterials.com/images/steel-images/small/steel/steel800/steel800-2.jpg','Model Photo'"
dummy=str(re.compile(r'.com'',,''(.*?)'',,''Model Photo').search(str(temp)).group(1))
Run Code Online (Sandbox Code Playgroud)
我不认为"虚拟"是正确的我不确定如何"逃避"regex re.compile命令中的单引号和双引号.
我试着谷歌搜索问题,但我找不到任何相关的东西.
将不胜感激任何指导.
谢谢.
在Python中处理包含转义字符和引号的字符串的最简单方法是对字符串(""")进行三重双引号并为其添加前缀r.例如:
my_str = r"""This string would "really "suck"" to write if I didn't
know how to tell Python to parse it as "raw" text with the 'r' character and
triple " quotes. Especially since I want \n to show up as a backlash followed
by n. I don't want \0 to be the null byte either!"""
Run Code Online (Sandbox Code Playgroud)
该r手段"采取逃避用作文字".三重双引号(""")防止单引号,双引号和双引号过早结束字符串.
编辑:我扩展了示例,包括\0和和\n.在普通字符串(不是原始字符串)中,a \(转义字符)表示下一个字符具有特殊含义.例如,\n意思是"换行符".如果你真的想要在你的字符串中\跟随n字符你就必须写\\n,或者只是使用原始字符串,就像我在上面的例子中所示.
您还可以在Python文档中阅读有关字符串文字的内容:
| 归档时间: |
|
| 查看次数: |
3180 次 |
| 最近记录: |