Notepad++ - regex : replace first occurrence of a

2020-04-19 07:00发布

问题:

Someone would know the regex expression can be used (in notepad++) in order to replace the first occurence of a characters group in each line?

eg:

abcdefg//ijkl//m.
qsdflkj//sdqlmkf//jqsmdl.

to

abcdefg\\ijkl//m.
qsdflkj\\sdqlmkf//jqsmdl.

so replace // by \\ in each line, but only the first occurence of , not the next.

If regex can't achieve it, is there another method with notepad. If not, I will code a program to split line and do the job, but need more time.

Thnaks in advance.

回答1:

Enter this regex in Find what field

(.*?)//(.*)

Enter this in Replace with field

$1\\$2

Select Regular expression in Search Mode and Uncheck . matches newline



回答2:

  • Ctrl+H
  • Find what: ^[^/]+\K//
  • Replace with: \\\\
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

^               : begining of line
  [^/]+         : 1 or more any character that is not a slash
  \K            : forget all we have seen until this position
  //            : 2 slashes

Replacement:

\\\\     : 2 backslashes, each one must be escaped

Result for given example:

abcdefg\\ijkl//m.
qsdflkj\\sdqlmkf//jqsmdl.