推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

用什么方法,能最快的把一行文字头尾的符号(...text...)去掉?

  •  
  •   miniyao · Oct 30, 2018 · 3091 views
    This topic created in 2926 days ago, the information mentioned may be changed or developed.

    文稿中,有很多文字是用括号(...text...)框起来的,括号前面有 # 的那一行,类似这样:

    #(...text...)
    

    须要把 # 连()一起剥去,剩下:

    ...text...
    

    没有 # 开头的那一行,括号(...text...)继续保留不动。

    20 replies  •  2018-11-05 17:02:53 +08:00
    neoblackcap
    neoblackcap
    Oct 30, 2018 via iPhone
    1
    自己写一个解析器,快速跑一下。
    不用解析的方法,可能会出错
    hqs0417
    hqs0417
    Oct 30, 2018
    2
    全文替换,正则表达式,比如说用 vscode
    ywgx
    ywgx
    Oct 30, 2018 via iPhone
    3
    对 vim 来说 录制个宏,播放即可,估计也就几秒吧
    cname
    cname
    Oct 30, 2018 via Android
    4
    sublime text 搜索#,选择所有,删除
    cname
    cname
    Oct 30, 2018 via Android
    5
    审题失败,打扰了😷
    lniwn
    lniwn
    Oct 30, 2018
    6
    任意一个支持正则的编辑器都能做到,楼主有尝试过吗?
    wsh1108
    wsh1108
    Oct 30, 2018 via Android
    7
    随便一个支持正则的编辑器
    cname
    cname
    Oct 30, 2018 via Android
    8
    应该是搜索#( 然后选择所有,删除,向右移动光标,删除)
    wsh1108
    wsh1108
    Oct 30, 2018 via Android
    9
    @lniwn 尴尬😅,看到你发,我就不回了
    Akkuman
    Akkuman
    Oct 30, 2018 via Android
    10
    notepad++正则替换 \#\((.*)\)替换为$1
    co3site
    co3site
    Oct 30, 2018 via Android
    11
    text.lstrip('#(').ratio(')')
    co3site
    co3site
    Oct 30, 2018 via Android
    12
    @co3site 手机打字,手残了。。。
    text.lstrip('#(').rstrip(')')
    sutra
    sutra
    Oct 30, 2018
    13
    $ cat a
    #(hello)world
    #(he(llo)world
    (hello)world

    $ cat a | sed 's/^#(\(.*\))/\1/'
    helloworld
    he(lloworld
    (hello)world
    sutra
    sutra
    Oct 30, 2018❤️1
    14
    上面的回复可能审题错误。

    $ cat a
    #hello(hello)world
    #hello(he(llo)world
    (hello)world

    $ cat a | sed 's/^#\(.*\)(\(.*\))/\1\2/'
    hellohelloworld
    hello(helloworld
    (hello)world
    loryyang
    loryyang
    Oct 30, 2018
    15
    解法同 13 楼,注意区分下中英文字符就行。好好学正则啊,非常有用
    ryd994
    ryd994
    Oct 31, 2018 via Android
    16
    就算不用正则,用 Python 也就是一个 for 一个 if 的事………
    TangMonk
    TangMonk
    Oct 31, 2018
    17
    aaaaasam
    aaaaasam
    Nov 5, 2018
    18
    1、vim 打开文件:%s/^\#(\(.*\))$/\1/
    aaaaasam
    aaaaasam
    Nov 5, 2018
    19
    1
    aaaaasam
    aaaaasam
    Nov 5, 2018
    20
    。。。ctrl + 回车不是换行。。。
    1、用 vim 打开文件,然后进入命令模式,输入 %s/^\#(\(.*\))$/\1/ 回车就好了

    2、sed -i '.bak' "s/^#(\(.*\))$/\1/" <filename> ;
    #上面的.bak 因为实在 Mac os 环境下才使用,其他的应该不需要
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4316 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 01:09 · PVG 09:09 · LAX 17:09 · JFK 20:09
    ♥ Do have faith in what you're doing.