V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
abctest
V2EX  ›  问与答

问一个 Python 正则表达式(将文本中所有 [] 内的第一个字符是大写字母的匹配的括号里的数字加上<sub></sub>标签)

  •  
  •   abctest · May 3, 2017 · 2809 views
    This topic created in 3292 days ago, the information mentioned may be changed or developed.
    比如: [ CH4 ] -> [ CH<sub>4</sub>]
    1 replies    2017-05-03 13:03:56 +08:00
    noqwerty
        1
    noqwerty  
       May 3, 2017
    ```python
    import re
    x = '[ CH4 ]'
    regex = re.compile(r'(\[\s?[A-Z].*?)(\d)(\s?\])')
    print(re.sub(regex, r'\1<sub>\2</sub>\3', x))
    # [ CH<sub>4</sub> ]
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5611 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    ♥ Do have faith in what you're doing.