Regular expression--Common replacement in Notepad++

Original URL: Regular Expressions-Common Replacement for Notepad++

Introduction

This article introduces some commonly used examples when Notepad++ uses regular expressions for replacement.

Formatting of server JSON

  • Example 1: Remove the carriage return and change it to the correct JSON format
    • search:
      • ([^,])(\r)(\n)(\s+)
    • replace
      • \1

remove blank lines

  • Method 1: Self-contained functions
    • Edit => Line Operations => Delete Empty Lines
  • Method 2: Search and replace
    • Search: ^\s+
    • replace:

remove trailing spaces

  • Search: [ ^t]+$
  • Replace: empty string

remove leading spaces

  • Search: ^[ ^t]+
  • Replace: empty string

Add "hehe" after all spaces  

Search: ( ) //There is a space inside the parentheses
Replacement: \1heheda

        Parsing: In regular expressions, groupings are placed in parentheses, which can be referenced by \1,\2...\9 (or $1,$2...$9) in order, and \0 is used for the entire regular expression to quote. For this place, \1 quotes the space matched by the parentheses, and then adds "hehe"

group replacement

  • Example 1: Change a sentence ending with "Chinese characters + numbers" to "Chinese characters + tab + numbers"
    • search:
      • ([\u4e00-\u9fa5]+)(\d*)($)
    • replace:
      • \1\t\2
  • Example 2: Add blank lines above and below the lines with numbers plus .
    • search:
      • (\n)(\d+)(\.)(.+)(\r)
    • replace:
      • \1\r\n\2\3\4\r\n\5

One line of English and one line of Chinese => Single line: English follows Chinese

  • search
    • ([A-Za-z]+)(\r)(\n)([\u4e00-\u9fa5]+)
  • replace:
    • \1\4

        Analysis: The carriage return and line feed in windows corresponds to \r\n , which can be understood as \r\n at the end of each line. If it is a blank line obtained by a carriage return and line feed, there is only one \r\n in the blank line. In this way, blank lines and non-blank lines can be combined into one \n\r, and finally the remaining characters are formed into \r\n. But you can't directly search for \r\n to replace it with null, because in this way, all carriage returns and line feeds will be deleted, causing lines to be directly connected together. In addition, the second method of deleting spaces will cause problems when undoing, and many more blank lines will be generated.

      \r\n corresponds to 0D and 0A respectively. Note: When performing hexadecimal display, 16 are displayed in one line, and D0 and A0 are not necessarily at the end of a line.

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/128143302