我有一个输入,我知道它有用,我希望能够替换字符串中的某些文本,但我需要解决它区分大小写。我试图将与“查找”变量匹配的任何内容小写,但我需要最好的方法。
# Replace Item Code Filter based on ColorArmor Filter by SethBling
# First Edit by Howzieky_10, correction and bug fixing by medi4
from pymclevel import MCSchematic
from pymclevel import TileEntity
from pymclevel import TAG_Compound
from pymclevel import TAG_Short
from pymclevel import TAG_Byte
from pymclevel import TAG_String
from pymclevel import TAG_Int
from pymclevel import TAG_List
from numpy import zeros
displayName = "Replace Command Block Text"
inputs = (
("Find", "string"),
("Replace", "string"),
("How many times?","string"),
)
def perform(level, box, options):
find = options["Find"]
replace = options["Replace"]
count = options["How many times?"]
for (chunk, slices, point) in level.getChunkSlices(box):
for te in chunk.TileEntities:
px = te["x"].value
py = te["y"].value
pz = te["z"].value
if px < box.minx or px >= box.maxx:
continue
if py < box.miny or py >= box.maxy:
continue
if pz < box.minz or pz >= box.maxz:
continue
if te["id"].value == "Control":
command = te["Command"].value
print find
print command
command = command.replace(find, replace)
te["Command"] = TAG_String(command)
print te["Command"].value
chunk.dirty = True
答案 0 :(得分:1)
这样的东西?
这只是在字符串'aBc'
中找到'ABCaBcabcAbC'
的小写字母。
>>> s = 'ABCaBcabcAbC'
>>> find = 'aBc'
>>> s.replace(find, find.lower())
'ABCabcabcAbC'
使用正则表达式:
>>> import re
>>> re.sub(r'{}'.format(re.escape(find)), lambda m:m.group().lower(), s)
'ABCabcabcAbC'
如果您正在寻找确切的字词,请使用此正则表达式\b{}\b.format(find)
。
答案 1 :(得分:0)
在查找时,只需小写整个字符串:
inputstring.lower().find(your_search_text.lower())
现在,您通过整个输入字符串不区分大小写。
inputstring
本身引用的值未更改,str.lower()
返回 new 字符串对象,其中所有字符均为小写:
>>> inputstring = 'Hello World!'
>>> inputstring.lower().find('hello')
0
>>> inputstring.lower().find('world')
6
>>> inputstring
'Hello World!'