简体   繁体   English

隔离字符串中的某些文本

[英]Isolate certain text in a string

I have this massive string in python我在 python 中有这个巨大的字符串

b'{\n "timestamp" : HIDDEN,\n "profileId" : "REDACTED",\n "profileName" : "REMOVED",\n "textures" : {\n "SKIN" : {\n "url" : "http://example.com/example/123example123",\n "metadata" : {\n "model" : "slim"\n }\n }\n }\n}'

and I was wondering if it was possible to isolate the http://example.com/example/123example123 even if the /123example123 part changed every time I ran the script.我想知道是否可以隔离http://example.com/example/123example123即使/123example123部分在我每次运行脚本时都发生了变化。

You can use regex您可以使用正则表达式

import re

# URL regex
regexp = r'((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*'

string = "http://example.com/example/123example123"
url = re.search(regexp, string).group()

url variable should contain your url even if it changes. url 变量应该包含您的 url 即使它发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM