【问题标题】:How do I delete a specific portion of a line that begins after a character of my choice?如何删除在我选择的字符之后开始的行的特定部分?
【发布时间】:2021-05-03 00:08:11
【问题描述】:

我想要一个有用的方法或脚本来删除几行的某个部分,但条件是我选择删除开始的字母。 比如在下面的文字中

  • 测试测试 |这是为了测试
  • 测试测试 |这是为了测试
  • 测试测试 |这是为了测试

我要选择符号 |删除它后面的所有行,使文本变成这样

  • 测试测试 |
  • 测试测试 |
  • 测试测试 |

【问题讨论】:

  • 您在这个问题上同时标记了 Python 和 PHP——这不是特定现有脚本的一部分吗?如果这是您需要作为独立工具的东西,我建议您使用sedcut,而不是编写自己的脚本。例如。试试echo "TestTest | This Is for Test" | sed -e "s/|.*/|/"

标签: python php string text


【解决方案1】:

我将展示一个使用拆分的示例。虽然我相信很多人会分享一些令人惊奇的正则表达式用法。这是干净和简单的。

a_string = "TestTest | This Is for Test"
split_string = a_string.split("|", 1)
substring = split_string[0]

输出

TestTest 

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 2020-06-17
    • 2020-05-10
    • 1970-01-01
    • 2019-11-17
    • 2020-11-17
    • 2013-12-08
    • 2013-08-05
    • 2015-11-12
    相关资源
    最近更新 更多