使用标点符号列表进行re.sub

发布于 2025-01-26 11:15:59 字数 300 浏览 4 评论 0原文

有没有办法列出标点符号,并将其放入re.sub中。我正在使用F-string文字,但逃生角色打破了一切。我应该手动键入它吗?

marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '|'.join(['\\' + f'{n}' for n in marks])
re.sub('\ |\?|\.|\!|\/|\;|\:', '', 'line')

Is there a way to take a list of punctuation, and put it into a re.sub. I was using f-string literals but the escape character breaks everything. Should I just type it manually?

marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '|'.join(['\\' + f'{n}' for n in marks])
re.sub('\ |\?|\.|\!|\/|\;|\:', '', 'line')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

婴鹅 2025-02-02 11:15:59

尽管我更喜欢使用角色类,但您的工作没有错。无法从您的帖子中100%确定,但听起来您正在尝试从字符串中删除某些标点符号?例子:

marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '[' + ''.join(['\\' + f'{n}' for n in marks]) + ']'

x = re.sub(punctuation, '', 'li..n?e-asdfa「sdf??ag\/as●dga,s|g`da')

print(punctuation)
print(x)

Nothing wrong with what you're doing, though I prefer to use a character class. Can't be 100% sure from your post but it sounds like you're trying to remove certain punctuation from a string? Example:

marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '[' + ''.join(['\\' + f'{n}' for n in marks]) + ']'

x = re.sub(punctuation, '', 'li..n?e-asdfa「sdf??ag\/as●dga,s|g`da')

print(punctuation)
print(x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文