Replacing multiple blank lines with one blank line using RegEx search and replace

后端 未结 10 1786
旧巷少年郎
旧巷少年郎 2020-12-13 09:31

I have a file that I need to reformat and remove \"extra\" blank lines.

I am using the Perl syntax regular expression search and replace functionality of UltraEdit a

相关标签:
10条回答
  • 2020-12-13 09:55

    I'm not sure what UltraEdit lets you get away with in the "replace" area, but if you cannot use a newline (I've had this problem before) but can use capture references, this might work:

    Find    : \s*(\r\n)\s*(\r\n)\s*\r\n
    Replace : $1$2
    

    Not tested extensively, but seems to work on the sample you provided.

    0 讨论(0)
  • 2020-12-13 09:56

    See this thread for what's causing the problem. As I understand it, UltraEdit regexes are greedy at the character level (i.e., within a line), but non-greedy at the line level (roughly speaking). I don't have access to UE, but I would try writing the regex so it has to match something concrete after the last blank line. For example:

    search:   (\r\n[ \t]*){2,}(\S)
    replace:  $1$2
    

    This matches and captures two or more instances of a line separator and any horizontal whitespace that follows it, but it only retains the last one. The \S should force it to keep matching until it finds a line with at least one non-whitespace character.

    I admit that I don't have a whole lot of confidence in this solution; UltraEdit's regex support is crippled by its line-based architecture. If you want an editor that does regexes right, and you don't want to learn a whole new regex syntax (like vim's), get EditPadPro.

    0 讨论(0)
  • 2020-12-13 09:56

    On my Intellij IDE what was search for \n\n and Replace it by \n

    0 讨论(0)
  • 2020-12-13 09:57

    Replacing

    \n\s*\n\s* 
    

    with

    \n\n
    

    should do the trick

    0 讨论(0)
  • 2020-12-13 10:00

    Should also work with spaces on blank lines

    • Search - /\n^\s*\n/
    • Replace - \n\n
    0 讨论(0)
  • 2020-12-13 10:02

    Try this perl oneliner perl -00pe0, if you want in place editing, just add -i option

    0 讨论(0)
提交回复
热议问题