Replace characters only when a certain number exist
I was wondering if it is possible to do an advance find and replace where NP++ looks at each line, counts the number of times a character exists and if certain criteria are met, do a replace.
I want to look for lines that have 5 pipes | and if 5 exist, replace number 1 and 3 with a comma. Is this possible?
I want to look for lines that have 5 pipes | and if 5 exist, replace number 1 and 3 with a comma.
I believe I have created a regex (regular exression) which will replace the numbers
3if there are 5
|pipes in a line. However some caveats.
- If more than 5 pipes it will still replace any 1 and/or 3 on that line.
- It needs to run multiple times as ONLY replaces 1 number on each line for each run. If your lines will ONLY have 1 of the numbers then possibly only 1 run required.
- As it uses the
\Kcombination it MUST be run with the “Replace All” button.
Given that, here is what I have:
As stated before it’s a regex so search mode must be “regular expression”. Keep clicking on the “Replace All” button until the bottom of the window states “Replace All: 0 occurances were replaced in entire file”.
See if that helps at all.
PS edit, I assumed the actual number 1 and 3 were to be replaced, however I think I see @Alan-Kilborn assumes that the 1st and 3rd instance of the pipe character are to be replaced. Which is it?
I assume, like @alan-kilborn, that you want change the first and third
|char, of any line, with a comma
,, ONLY IF current line contains, at least, five
I propose this regex version :
You may use as well, for the search regex, the free-spacing mode, below, with some explanations , in comments. Just select all that block and open the Replace dialog (
Ctrl + H)
(?x) # FREE-SPACING mode ^ # BEGINNING of line ( # START Group 1 [^|\r\n]* # ANY range, possibly NULL, of chars, DIFFERENT from PIPE | and EOL chars, till ... ) # END Group 1 \K # RESET of current MATCH, so far \| # ... a LITERAL PIPE char ( # START Group 2 (?1) \| (?1) # "Group 1 REGEX", followed by a LITERAL PIPE char and followed by an other "Group 1 REGEX", till ... ) # END Group 2 \| # ... a LITERAL PIPE char (?= (?2) \| ) # ONLY IF "Group 2 REGEX", followed by a LITERAL PIPE char ( The FIFTH PIPE char ) is found, RIGHT AFTER
Whatever the search syntax used, You must click, exclusively, on the
Replace Allbutton ( Do not use the
Regular expressionsearch mode, of course
Tick, preferably, the
@Terry-R First and third instance of pipe
Am unsure if this will have worked but it causes my NP++ to crash. I updated to the latest version as I knew that I was a few versions behind but this gave the same result.
I don’t see a Notepad++ crash with @guy038 's regex:
In fact, I don’t know of any regex you could specify that has been known to cause a N++ crash before??
But anyway, RegexBuddy complains about the regex, saying for the second
(?1)that: Recursive calls need to be optional or the called group needs an alternative without recursion for the group to be able to match anything
…so, I think there is something funky going on with this.
To @alan-kilborn :
I don’t really understand the
Below, I indicated :
In the second line, where the two groups
In the third line, the regex
In the fourth line, where the subroutine calls are used
Definitions: Gr_1 Gr_2 _________ __________ Regex: ^([^|\r\n]*)\K\|((?1)\|(?1))\|(?=(?2)\|) ¯¯¯¯ ¯¯¯¯ ¯¯¯¯ Utilizations: Gr_1 Gr_1 Gr_2
And, we cannot see any subroutine call
(?#), located within its own group
(...). So this clearly shows that this regex does not use any recursive syntax !
In order to get a recursive syntax, you need, for instance, this kind of regex construction :
.......(......(?1)..)..., with no parenthesis in any “dot” zones, with the
(?1)located inside the parentheses zone defining the Group
To all :
Moreover, I tried with a small set of lines with pipe chars that I duplicated many times in order to get a
12,397,000bytes file, containing about
Applying the regex S/R, with the free-spacing mode, against that file was successful, after
57 s, due to my old
Win XP SP3machine ;-) And, of course, N++ did not crash at all !
In the short regex form, without the free-spacing mode, I simply omitted the useless modifier
(?x), as there is no space char !
I don’t really understand the RegexBuddy comment
Ha. Me either!
Hi, @alan-kilborn and All,
To be more accurate :
As a matter of fact, the
RegexBuddy’s statement “Recursive calls need to be optional OR the called group needs an alternative without recursion for the group to be able to match anything” is totally exact
But I still do not see how this could apply to my regular expression, given the observations mentioned in my previous post. !
I tried to run it on another machine and it executed successfully. I suppose I should have tried the first ;-)
Anyway, thank you very much for your help