Really the only tricky part is zero-padding a number to three digits. Here’s how that is done. The find-what expression will capture one, two or three digits as follows:

if one digit is present, it will be remembered as group-3 (group-1 and group-2 will be undefined)

if two digits are present, they will be remembered (first digit as group-1, second digit as group-3, group-2 will be undefined)

if three digits are present, they will be remembered, in order, as group-1, group-2, and group-3

This part of the replace regex:

(?2\1\2\3:0(?1\1\3:0\3))

does the following:

test if group-2 is defined, we have 3 digits in the original match, so insert remembered group-1 digit followed by group-2 digit followed by group-3 digit; we are DONE

if group-2 is NOT defined, insert a 0, and then:
test if group-1 is defined, we have 2 digits in the original match, insert remembered group-1 digit and group-3 digit; we are DONE
if group-1 is NOT defined, we have one digit in the original match, so insert another 0 (now there are a total of two zeroes), and insert remembered group-3 digit

If the two groups 2 and 3 are, both, defined ( case number with 3 digits ), we rewrite the P string, only, followed by the number
( Group 1 )

If the group 2 is defined and group 3 is NOT defined ( case number with 2 digits ), we rewrite the P0 string, followed by the number
( Group 1 )

If the two groups 2 and 3 are, both, NOT defined ( case number with 1 digit ), we rewrite the P00 string, followed by the number
( Group 1 )

Now, I think that the first part hyperlink="history:/ and the last part |view:history:HistoryChart">, of each line, do NOT need to be part of the S/R regexes, as they are unchanged, after replacement. So, we could, simply, use the following S/R :

SEARCH (?-si)MS_MSCP_3_P2_2/.+C((\d)?(\d)?\d)

REPLACE :SITE00002_L011_O062/P(?2:0)(?3:0)\1

As usual, the (?-si) modifiers, at beginning of the search regex, means that :

The dot, special regex character, stands for any single standard character, only ( -s )

I like your regex for this better than mine. I don’t really care that it is shorter; I like it because yours is more extensible (easier to see how to extend it to zero-pad out to 4, 5, etc. digits).

As to eliminating the “extra” text from the search and replace string, this is a tough call. In proposing a solution, I don’t like to assume too much (but sometimes you have to!) about what the questioner is doing; thus I err on the side of caution and leave in stuff like that. It should be pretty easy for the questioner to decide to remove that extra plaintext from their specific use case, if appropriate, once they start experimenting with solutions posed. :-D

I’m about to give it a go so will let you know how it goes. Just have to read it 20 times or so to process your thinking/coding language.

Thank you so much for your replies.

]]>https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/6https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/6Mon, 26 Jun 2017 21:54:59 GMTSuccess! With some minor alterations to your feedback, you guys have chopped possibly a 3-4 week job down to a few days!

This is powerful stuff…in the right hands/minds!

FYI, the alteration required was to add back in the hyperlink="history:/ to the (?-si)MS_MSCP_3_P2_2/.+C((\d)?(\d)?\d) search, but only due to the fact that there are other instances of MS_MSCP_3_P2_2/ throughout the code (which I did not tell you about!).

In addition, you’ll find good documentation, about the new Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by Notepad++, since its 6.0 version, at the TWO addresses below :

]]>https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/9https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/9Tue, 04 Jul 2017 19:58:33 GMTAnother way of inserting (missing) leading zeros for numbers. This takes two search-and-replace operations. The first step is to insert the wanted number of leading zeros at the front of every number. The second step is to remove any unneeded zeros.

For the example, where numbers matching C1| are to be changed to P001|, i.e. adding two zeros.
First step: Replace (C)(\d{1,2}\|) with \100\2. (As Notepad++ only allows nine groups there is no ambiguity with the \100 part, it means \1 then 00.)
Second step: Replace (C)0+(\d{3}\|) with \1\2.

I didn’t check your proposed solution, but rather I just wanted to point out that Notepad++ can handle more than 9 captured groups. For example, there’s a regex replacement in this thread that uses 52 capture groups!

Indeed, your method looks better and more simple. In addition, I succeeded to simplify your two regex S/R :-))

So, let’s start with the original text, below, with some numbers, preceded by the letter C :

C12345
C123456
C123
C1234567
C1234
C12
C12345678
C1

I omitted the last| character, which is useless, for our discussion. Now, as we’re searching for a formatted list of eight digits numbers, we need to insert a seven0's string, right after the letter C. To do so, I use the following S/R :

As the search is only a look-behind construction, it matches the zero-length position, right after the letter C

And, at that position, it simply inserts, in replacement, the 0000000 string !!

Now, to get the aligned table of numbers, padded out with some 0's, I chose the following S/R, which suppresses the unnecessary 0, rather than rewriting the letter C and the different numbers to keep !

As usual, the modifier(?-s), ensures that the special dot character will match standard characters, only !

The search regex looks for any amount, even empty, of standard characters ( .* ), if two conditions are true :

This range of characters must be preceded by the C letter ( (?<=C) )

This range of characters must be followed by an eight digits number ( (?=\d{8}) )

As the replacement part is EMPTY, this range is just deleted

To sump up, in order to obtain an aligned list of n digits numbers, padded out with a particular character :

Choose the fix string, located right before the padded characters to insert. Note that the first replacement zone could have contained nspaces or ndots or any other padded character !

Repeat the look-behind in the second search zone and use the (?=\d{n})look-ahead

As you see, Adrian, it’s a good lesson ! Very often, two simple consecutive S/R are better that a single complicated one :-D

Best Regards,

guy038

]]>https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/12https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/12Mon, 30 Oct 2017 19:13:08 GMTThanks for the clarification @Scott-Sumner. My wording may have been poor; Notepad++ allows more than nine groups but the backslash only allows nine.

]]>https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/13https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/13Tue, 31 Oct 2017 08:52:48 GMT@guy038 I have seldom used look-behinds or look-aheads and I do not remember their syntax. For the times I have needed complicated search and replaces the performance difference between the non-look-behind(or ahead) form and the form with look-behinds(or aheads) is much less than the mental effort it would take me to change my approach. Having said that, I am very happy that you have found another, possibly neater, way of using my idea.

]]>https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/14https://notepad-plus-plus.org/community/topic/13988/find-replace-issues/14Tue, 31 Oct 2017 09:00:39 GMTHello sorry to jump onto this thread but it thought it better then starting a new one as my issue is related.

So the part that makes your situation ugly is that I presume there can be a variable amount of :xxxx in your real data–you didn’t say… For example, maybe all of the following are valid things you want to match for replacement:

If this is NOT the case and you always have SIX sets of :xxxx then the situation is a lot less ugly. But, moving forward with a variable (but bounded) count (from 1 to 10 occurrences, for example), try this [and PLEASE use copy-n-paste… :-) ]:

What this is doing is matching whatever follows the individual colons (after your "Prod_Data leading text is matched) into the capture groups 1-10. At replacement time, the colons (converted to backslashes) plus the captured groups are conditionally inserted into the output stream. The conditional syntax is necessary because of the variable number of occurrences of \xxxx that might be needed.

Thus, each (?::(.+?))? in the FW string captures a :xxxx – the second occurrence of : in this is the real/literal colon…the first colon is part of (?: which is just syntax saying “group the stuff in the parentheses but don’t capture it for later use”.

And each (?y\\${y}) (where y = 1…10) in the RW string represents a backslash and the original xxxx. When group 8 (for example) doesn’t exist the syntax (?8 will evaluate to false and whatever occurs between the (?8 and the next)` will NOT be part of the replacement data.

Yea, it’s the difference between wanting to help and making too many/few assumptions about a questioner’s data. In this case I wouldn’t go so far to assume that colons only appear in these places, but who knows?

I’ve studied the general case of searching a specific character, ONLY IF, located inside a range of characters with delimiters.

Now, two cases are possible :

Case A : an area with a same starting and ending character, as, for instance, '.....' or "....."

Case B : an area with a different starting and ending character, as, for instance, (.....), [.....], {.....} or <.....>

Notes :

For our discussion, we are supposed to look for the colon character

For case A, I chose the double quotes delimiter ", as common boundary

For case B, I chose the start delimiter< and the end delimiter>

Let’s begin with the easier form !

Case B : A possible regex would be :

SEARCH :(?=[^<\r\n]*>)

REPLACE Any string or character, even EMPTY

Note that this regex looks for a colon character, ONLY IF followed by a range, possibly empty, of characters, different from the first delimiter < and from the EOL characters \r and \n , till the ending delimiter >

On the test example, below, the regex finds all colon characters, located inside the <.....> areas exclusively ( the ones which are underlined ) Fine !

Now, in case A, the annoying thing is that it’s impossible to distinguish the two delimiters ! So, we’re going to cheat a bit ! First, we’ll replace, for instance, any area "....." by an oriented area as, for instance, #"....."@. Of course, these new boundaries must be absent from the present contents of the file !