【问题标题】:Remove some semi colons in a data file删除数据文件中的一些分号
【发布时间】:2019-01-20 11:03:49
【问题描述】:

您好,我想删除我的数据文件中的一些分号,如下所示:

a;bfcse;g
g;qdq;d;u
q;g;sd;;o
c;dd;ea;b
w;;rz;z;m

我想使用正则表达式来实现(因为我尝试选择一列并进行替换,但它不起作用,notepad++ 中的按钮在我的选择中是灰色的,无法替换):

a;bfcse;g
g;qdqd;u
q;gsd;o
c;ddea;b
w;rzz;m

我认为像子字符串这样的东西可能是可行的,但是使用 notepad++ 和 sublimetext 就很难了...

你有什么想法吗?

非常感谢!

编辑:但我认为很清楚我想要做的是在这个区域中删除分号:

  -------
a;|bfcse|;g
g;|qdq;d|;u
q;|g;sd;|;o
c;|dd;ea|;b
w;|;rz;z|;m
  -------

【问题讨论】:

  • 这里的逻辑是什么?第一个例子是你有的,第二个是你想要的吗?您能否添加您尝试过的内容以及应该匹配和替换的内容的清晰示例?
  • 所以你想要的结果中的第二行 g;qd;d;u 是错误的?
  • 不,这是正确的,因为我有g;qdq;d;u,然后我在分号上方的区域删除了g;qd;d;u
  • 您删除了第二个 q 而不是分号?
  • 为什么g;qdq;d;u 变成g;qd;d;u?根据您的要求“在该区域中删除分号”,它应该是g;qdqd;u

标签: python regex notepad++ sublimetext3 sublimetext2


【解决方案1】:

使用记事本++

  • Ctrl+H
  • 查找内容:(^.+?;|\G)(.*?);?(?=.*?;.+?$)
  • 替换为:$1$2
  • 检查环绕
  • 检查正则表达式
  • 取消选中. matches newline
  • 全部替换

说明:

(               # group 1
  ^             # beginning of line
  .+?           # 1 or more any character but newline, not greedy
  ;             # a semicolon
 |              # OR
  \G            # restart from last match position
)               # end group 1
(.*?)           # group 2, 0 or more any character but newline, not greedy
;?              # optional semicolon
(?=             # start lookahead, make sure we have after:
  .*?           # 0 or more any character but newline, not greedy
  ;             # a semicolon
  .+?           # 1 or more any character but newline, not greedy
  $             # end of line
)               # end of lookahead

替换:

$1          # content of group 1
$2          # content of group 2

给定示例的结果:

a;bfcse;g
g;qdqd;u
q;gsd;o
c;ddea;b
w;rzz;m

【讨论】:

    【解决方案2】:

    我将创建一个新变量,并添加所有不是分号的内容,如下所示:

    nosemicolons = ('')
    somesemicolons = ('''  -------
    a;|bfcse|;g
    g;|qdq;d|;u
    q;|g;sd;|;o
    c;|dd;ea|;b
    w;|;rz;z|;m
      -------''')
    while len(somesemicolons) != 0:
        if somesemicolons[:1] != ';':
            nosemicolons = nosemicolons + somesemicolons[:1]
            somesemicolons = somesemicolons[1:]
        else:
            somesemicolons = somesemicolons[1:] #placeholder
    print(nosemicolons)
    

    哪些输出:

      -------
    a|bfcse|g
    g|qdqd|u
    q|gsd|o
    c|ddea|b
    w|rzz|m
      -------
    

    遗憾的是,它看起来不像一张桌子。您可以更改它,以便在输出中添加 ' ' 而不是什么都没有(请参阅占位符)。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      相关资源
      最近更新 更多