简体   繁体   中英

Notepad++ regex find and replace empty space and end of line

want to find "blank" replace it with "," start doing the same thing on the next line.

using notepad++, I have a file 100s of lines (IP addresses):

192.168.10.1 192.168.10.2
192.168.10.3 192.168.10.4

ctrl+h find "" replace with,

192.168.10.1,192.168.10.2,
192.168.10.3,192.168.10.4,

it is very simple to find empty space and replace it with a comma",", also it I know how to find the end of the line and put a comma. however, I am struggling to tell notepad++ in the replace field to also put a comma at the end of the line. OR in the find what filed to find empty space and end of line"$".

Use

\h+|$

Replace with , . See the regex demo .

Details

  • \h+ - 1 or more horizontal whitespace
  • | - or
  • $ - end of a line.

Need a little more work to avoid possible ,, at end of line.

(?m)(?:\h+|$(?<!\h)) replace with a comma

https://regex101.com/r/8r5ajs/1

Expanded

 (?m)                          # Modifier, multi-line    
 (?:
      \h+                           # Many Horizontal whitespace
   |                              # or,
      $                             # End of line
      (?<! \h )                     # If not next to a horizontal whitespace
 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM