所以我有一个字符串
"sdfsfsdfdsfsdfsd #.split.#style=background: black;"
我想对它做一些操作
我想将#.split.#
部分更改为div close标记并打开标记:
str.replace("#.split.#", '</div><div>')
除此之外,我想在style=
之后选择并放入div开放标签
这样做的快速程序是什么?
请记住,#.split.#style=
部分可以在字符串中多次出现,并且style=
之后的部分可以在长度上变化,但始终以换行符结束。
修改
我想要实现的是来自这样的字符串:
"sdfsfsdfdsfsdfsd #.split.#style=background: black;
sdfsfsdfdsfsdfsd #.split.#style=background: red;"
到此:
'sdfsfsdfdsfsdfsd </div><div style="background: black;">
sdfsfsdfdsfsdfsd </div><div style="background:red;">'
答案 0 :(得分:0)
按照你的所作所为,
/* 1 */
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"contents" : {
"$size" : 2
}
},
{
"topic" : {
"$eq" : "BBB"
}
},
{
"contents.headerName" : {
"$in" : [
"Program",
"Start Year"
]
}
},
{
"contents.value" : {
"$in" : [
1989,
"BBB"
]
}
}
]
},
"winningPlan" : {
"stage" : "KEEP_MUTATIONS",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"contents" : {
"$size" : 2
}
},
{
"contents.headerName" : {
"$in" : [
"Program",
"Start Year"
]
}
},
{
"contents.value" : {
"$in" : [
1989,
"BBB"
]
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"topic" : 1,
"contents" : 1
},
"indexName" : "topic_1_contents_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"topic" : [
"[\"BBB\", \"BBB\"]"
],
"contents" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : []
}
}
这将得到以下结果:
# Avoid str, it is a function name
text = "sdfsfsdfdsfsdfsd #.split.#style=background: black;"
text.replace("#.split.#", "</div><div ")
# End the tag
text += ">"
修改强>
分开并重新加入。
>>> text
"sdfsfsdfdsfsdfsd </div><div style=background: black;>"
<强> RE-修改
# Avoid str, it is a function name
text = 'sdfsfsdfdsfsdfsd #.split.#style=background: black;\n'
# Split string into sections
text = text.split('\n')
# Do the same thing on each line
for i, s in enumerate(text):
# Start div tag
new_text = s.replace('#.split.#', '</div><div ')
# Add quotes
new_text = new_text.replace('style=', 'style="')
new_text = new_text.replace(';', ';"')
if i != len(text) - 1:
new_text += '>'
# Apply changes
text[i] = new_text
# Join the strings back together with newlines
text = '\n'.join(text)
答案 1 :(得分:0)
以下是解决方案:
import re
text="sdfsfsdfdsfsdfsd #.split.#style=background: black\nsdfsfsdfdsfsdfsd #.split.#style=background: red"
matches = re.findall('.*#.split.#.*', text)
final = ""
for m in matches:
parts = re.search("(.*)#.split.#style=(.*)", m)
new = '{}</div><div style="{};">'.format(parts.group(1), parts.group(2))
final += new + "\n"
print final
,结果将是:
sdfsfsdfdsfsdfsd </div><div style="background: black;">
sdfsfsdfdsfsdfsd </div><div style="background: red;">