python - How can I replace the first occurrence of a character in every word?
问题描述
How can I replace the first occurrence of a character in every word?
Say I have this string:
hello @jon i am @@here or @@@there and want some@thing in '@here"
# ^ ^^ ^^^ ^ ^
And I want to remove the first @
on every word, so that I end up having a final string like this:
hello jon i am @here or @@there and want something in 'here
# ^ ^ ^^ ^ ^
Just for clarification, "@" characters always appear together in every word, but can be in the beginning of the word or between other characters.
I managed to remove the "@" character if it occurs just once by using a variation of the regex I found in Delete substring when it occurs once, but not when twice in a row in python, which uses a negative lookahead and negative lookbehind:
@(?!@)(?<!@@)
See the output:
>>> s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
>>> re.sub(r'@(?!@)(?<!@@)', '', s)
"hello jon i am @@here or @@@there and want something in 'here"
So the next step is to replace the "@" when it occurs more than once. This is easy by doing s.replace('@@', '@')
to remove the "@" from wherever it occurs again.
However, I wonder: is there a way to do this replacement in one shot?
解决方案
我将对以下模式进行正则表达式替换:
@(@*)
然后只需替换第一个捕获组,它都是连续的@符号,减一。
这应该捕获@
每个单词开头出现的每个单词,无论是字符串开头、中间还是结尾的单词。
inp = "hello @jon i am @@here or @@@there and want some@thing in '@here"
out = re.sub(r"@(@*)", '\\1', inp)
print(out)
这打印:
hello jon i am @here or @@there and want something in 'here
推荐阅读
- android - 如何获取项目中存在的单元测试的计数
- bash - 如果在 file2 中找不到字符串,则用空格替换 file1 中的字符串
- docker - 使用 docker 安装传统的基于 MVC 的 Web 应用程序有什么好处吗?
- python - Django 管理命令:“命令”对象没有属性“元”
- javascript - 无法在odoo js中继承一个类
- windows - 如何确保 az kubectl 凭据不会过期并且 kubectl 可以在生产环境中由脚本运行?
- node.js - findById 返回文档列表而不是单个结果
- javascript - Nativescript 选项卡视图更改不起作用
- vue.js - 有没有办法修复无法读取未定义的属性“帖子”?
- android - android的Camera 2 API中的预捕获序列是什么