Notepad ++ find and replace issue

Notepad ++ find and replace issue

I have had some luck with notepad ++ solving some complex find and replace a lot of similar text in around 8000 documents, but not this particular one:

In a text file I will have a lot of lines that say:

ABKNUMBER001=30422

and I need to change this to:

ABKNUMBER001=6543230422

I need the find and replace to be sure it's only replacing the line if ONLY 5 characters beginning with 3 exist after the '='

For example, this line below would not need to be changed:

ABKNUMBER001=304221

In fact, what I really need is to find anything that matches the lines below where xxx can be any digits 0-9 and yyyy can also be any digits 0-9 (but limited to just 4 digits), as follows:

ABKNUMBERxxx=3yyyy

My list will have entries ABKNUMBER001, 002...999 in each text file and I want to replace any of those 999 entries per text file that have a match for 3yyyy with 654323yyyy - make sense?

答案1

I want to replace any of those 999 entries per text file that have a match for 3yyyy with 654323yyyy

  • Menu "Search" > "Replace" (or Ctrl + H)

  • Set "Find what" to ^(ABKNUMBER[0-9][0-9][0-9]=)(3[0-9][0-9][0-9][0-9])$ or

  • Set "Find what" to ^(ABKNUMBER[0-9]{3}=)(3[0-9]{4})$

  • Set "Replace with" to \1654\2

  • Enable "Regular expression"

  • Click "Replace All"

    enter image description here

Before:

ABKNUMBER001=30422
ABKNUMBER002=40422
ABKNUMBER004=50422
ABKNUMBER005=60422
ABKNUMBER001=304221

After:

ABKNUMBER001=65430422
ABKNUMBER002=40422
ABKNUMBER004=50422
ABKNUMBER005=60422
ABKNUMBER001=304221

Further reading

答案2

Notepad++ does Perl regular expressions. This has, in particular, repetition operators:

ABKNUMBER[0-9]{3}=3[0-9]{4}$

I replace your xxx with [0-9]{3} to request exactly 3 digits and yyyy with [0-9]{4} to request exactly 4 digits. In addition I terminate with $ to match the end of the line. You may want to insert a [ ]* in front of the $, in case you are not sure that there are no stray space at the end of the lines.

This answer is based on what I know of regular expressions. I have no np++ at hand to test this, so please test first.

相关内容