Vogue (@voguemagazine) • Instagram photos and videos
Fashionista (@fashionista_com) • Instagram photos and videos
The Business of Fashion (@bof) • Instagram photos and videos
我解析了 <title>
中的字符串Instagram 页面中的标签。
我需要解析屏幕名称,它是 (@....)
之前的所有字符串在上面的字符串中。
对于上面的示例,它将是 Vogue
, Fashionista
,和The Business of Fashion
分别。
我尝试过类似的事情
string.split(' ')[0].replace('\n', '')
但这只是解析第一个标记。
最佳答案
模块“re”会有所帮助。请在下面找到使这成为可能的模式:
import re
pattern = re.compile("(.+?) \(@.*?\)")
string = "Vogue (@voguemagazine) • Instagram photos and videos"
word = pattern.findall(string)[0]
在模式“(.+?)\(@.*?\)
”中
(.+?)
- 捕获空格(“”)和括号之前的所有字符;
\(@.*?\)
- 捕获 括号中的内容(即“(\
”和“\)
”之间),例如“@
” 以及所有其他字符(“.*?
”)
关于python - 字符串解析为特定字符(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53369150/