【发布时间】:2025-12-28 00:30:07
【问题描述】:
我可以用\{\%(.*?)\%\}把hell0 {% my text %}改成hello
或<!--.*?/--> 将useful (<!-- remove it /-->)useful 更改为useful useful
我的问题是我想删除final \text { whatever here } result 中的任何内容,包括\text。所以它变成了final result。
我尝试了与r"\\text .*?/ }"相同的方法,但没有奏效。
我有一个代码,它是清理我的数据的类的一部分:
def get_features(self,s:str)->list:
'''
Produce Shingles or n-Grams of CHARACTERS in a given string.
args:
s: Given String
out: Shingle os a string. If a string is 'how are you' then the returned list is ['how','owa','war','are','rey','eyo','you',] with width = 3
'''
assert self.args_flag, "pass in the arguments for preprocessing by calling set_preprocess_params()"
if self.lower:
s = s.lower()
if self.ascii_only:
s = re.sub(r"[^\x00-\x7F]",'',s)
if self.remove_special: # Remove special characters
s = re.sub(r'[^\w ]+', '', s)
s = re.sub(r'[_ \\]', '', s) # Remove Empty spaces and _ as they are not covered in special chars. Also, I want to remove any "backslashes \"
return s
【问题讨论】:
-
re.sub(r'\s*\\text\s*{[^{}]*}', '', s)是否有助于解决问题? -
re.sub('(?<=final )(.*)(?=result)', '', 'final \text { whatever here } result')? -
能否请您查看以下答案并提供反馈?