【问题标题】:Split string on "$" using regex [duplicate]使用正则表达式在“$”上拆分字符串[重复]
【发布时间】:2021-06-01 14:05:01
【问题描述】:

我正在尝试在 $ 符号上使用正则表达式拆分字符串,但输出不是我想要的。

string = "43$hello"
list_of_splits = re.split("$",string)

Output:
['43$hello','']

Output I want:
['43','hello']

从输出中可以看出“$”是正则表达式中的一个特殊字符,但现在我该怎么做呢?

【问题讨论】:

标签: python regex split


【解决方案1】:

使用转义字符 \ : list_of_splits = re.split("\$", str)

【讨论】:

    【解决方案2】:

    你可以使用字符串拆分方法。

    string = "43$hello"
    string.split("$")
    

    输出

    ['43', 'hello']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2017-02-23
      相关资源
      最近更新 更多