У меня есть длинная строка, которую я хочу отфильтровать с помощью регулярного выражения
<@961483653468439706> Text to remove, this text is useless, that's why i want it gone!
i want this: `keep the letters and spaces`
Я хочу сохранить текст между символами `
единственная проблема заключается в том, что между каждым символом в той части строки, которую я хочу, есть невидимый символ. вы можете увидеть невидимые символы в regex101: https://regex101.com/r/rAYrMT/1
`([\'^\w]*)`
Итак, вкратце: оставьте все между `, кроме невидимых символов, информацию о которых можно найти здесь: https://apps.timwhitlock.info/unicode/inspect?s=%EF%BB%BF
Как получается строка? Похоже, что он был неправильно декодирован, и в этом случае его правильное декодирование, вероятно, решит проблему.
Вы можете отфильтровать непечатаемые символы:
import re
from string import printable
# your invisibles are in the string...
s='''<@961483653468439706> Text to remove, this text is useless, that's why i want it gone!
Type `keep the letters and spaces` and `this too`'''
for m in re.findall(r'`([^`]*)`', s):
print(repr(m))
print(''.join([c for c in m if c in printable]))
print()
Отпечатки:
'k\ufeffe\ufeffe\ufeffp\ufeff \ufefft\ufeffh\ufeffe\ufeff \ufeffl\ufeffe\ufefft\ufefft\ufeffe\ufeffr\ufeffs a\ufeffn\ufeffd s\ufeffp\ufeffa\ufeffc\ufeffe\ufeffs'
keep the letters and spaces
'this too'
this too
Вам не нужно использовать регулярное выражение для этого:
text = "<@961483653468439706> Text to remove, this text is useless, that's " \
"why i want it gone!Type `keep the letters and spaces`"
# put your invisible character between the first quotation marks here. obviously, they
# don't show up in this post.
filtered = text.replace('', '')
# because the passage you want is always between ``, you can split it and know that every
# second item in the list that split returns must be what you are looking for.
passage = filtered.split('`')[::2]
print(passage)
Что вы пытались решить это?