dongzj2015 2014-05-30 23:36
浏览 107
已采纳

仅删除特定字符之间的空格

I need to remove some white spaces between specific characters only. This is my data:

#1 2 5 1#
#   5 5 3#: 100% #5 55#
#554     #: 23    
#559#: 30%
#4 79#: 2 0 0#%
#473#: 20 #47  4#
#4  7  4 43 33 5 5#: 15s
#4 79#: 195%
#473#: 20 # 474#
#475#: 14.5s

I want all of the whitespaces between two # # tags, that are not separated by anything other than numbers, removed. My data should look like this:

#1251#
#553#: 100% #555#
#554#: 23    
#559#: 30%
#479#: 2 0 0#%
#473#: 20 #474#
#474433355#: 15s
#479#: 195%
#473#: 20 #474#
#475#: 14.5s

I'm currently trying to use the following regex in PHP to preg_replace those whitespaces, however, everything I've tried was unsuccessful.

(?:[#])(?:([\s])*[0-9]*)*(?:[#])

Regular expression visualization

Debuggex Demo

  • 写回答

2条回答 默认 最新

  • dt2002 2014-05-30 23:48
    关注

    This regex targets all the spaces you want. You can preg_replace them with empty strings.

    (?:#|\G)\d*\K\s+(?=[\d\s]*#)
    

    See demo

    As a second option, since your lines seem to be starting with #, to avoid an imbalance building up between the #, you can also use:

    (?m)(?:^#|\G)\d*\K\s+(?=[\d\s]*#)
    

    Explanation

    (?:#|\G)\d*\K\s+(?=[\d\s]*#)
    
    • Match the regular expression below (?:#|\G)
      • Match this alternative (attempting the next alternative only if this one fails) #
        • Match the character “#” literally #
      • Or match this alternative (the entire group fails if this one fails to match) \G
        • Assert position at the start of the match attempt (the start of the string for the first attempt) \G
    • Match a single character that is a “digit” (0–9 in any Unicode script) \d*
      • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
    • Keep the text matched so far out of the overall regex match \K
    • Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) \s+
      • Between one and unlimited times, as many times as possible, giving back as needed (greedy) +
    • Assert that the regex below can be matched, starting at this position (positive lookahead) (?=[\d\s]*#)
      • Match a single character present in the list below [\d\s]*
        • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
        • A “digit” (0–9 in any Unicode script) \d
        • A “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line) \s
      • Match the character “#” literally #
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错