简体   繁体   中英

Notepad++ replace all using regular expression

I have lines of numbers

16
18
19
21
24
25
26
30

How can I put commas at the end of each number using regular expressions. For example: 16 will turn to 16, and 18 will turn to 18, and so on

查找:([0-9] +)替换为\\ 1,

Find: (^[0-9]+$) (means the whole line is all digits - and capture it)
Replace: \\1, (means the first captured group then a comma

The question is not completely clear to me.

1. Only digits in a row and nothing else

Then Bohemians answer is working.

^(\d+)$

and replace with \\1, .

The ^ anchors the sequence of digits to the start of the row and the $ to the end.

2. The digits can be anywhere in the row together with other stuff

Then tafoo85 answer is working:

(\\d+) and replace with \\1, .

But this will replace also " tafoo85 " with " tafoo85, " and " 2fast4you " with " 2,fast4,you "

To avoid this behaviour and matching only "standalone" numbers, you would have to use word boundaries but those are not available in Notepad++.

Because Notepad++ regexes are very limited you would have to workaround this issue in four steps:

  1. ^(\\d+)$ and replace with \\1,
  2. ^(\\d+)(\\s) and replace with \\1,\\2
  3. (\\s)(\\d+)(\\s) and replace with \\1\\2,\\3
  4. (\\s)(\\d+)$ and replace with \\1\\2,

3. Change only digits at the start of the row

use only the start of the row anchor ^

^(\\d+) and replace with \\1, .

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