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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动