Help find 2nd occurrence and replace
-
Hi, @cesar-silva, @terry-R, @astrosofista, @alan-kilborn and All,
Very clever, indeed. You’are right about it : this look-ahead, alone, is enough to, precisely determine the position of the zero-length string ;-))
Your remarks are pertinent, too !. Finally, the best regex S/R, regarding the OP, should be :
SEARCH
(?-s)(?=;\d\d:)
REPLACE
-TTT
At least
4
people needed to reach the pantheon of regular expressions, pheew ;-))Cheers,
guy038
-
But now
(?-s)
is no longer needed. :-)For readers that don’t know why that is true: It is only needed if a
.
appears in the regular expression. -
-
@Alan-Kilborn said in Help find 2nd occurrence and replace:
Don’t forget your leading (?-s) or detail a setting for the . matches newline checkbox. I find the former easier.
Oh, no, too much typewriting - it’s sunday morning :)
Now, getting serious, you’re right on general cases, but in this particular one, given the data structure, that modifier has no effect at all. OP could check the
. matches newline
option but the output would be exactly the same.Of course, in other cases or situations it could do a lot of harm. Granted.
-
@astrosofista said in Help find 2nd occurrence and replace:
but in this particular one, given the data structure
Yep, given the data structure…
…but what if what the OP has shown is only part of a huge file, with vastly different types of data in different sections of the file? The user does a Replace All, verifies the section that they are currently looking at (cool, looks good!), doesn’t notice the other sections, saves, quits, they a few days later notices all of the unintended replacements in other sections of the file. Too late!
It’s better to be safer, rather than potentially sorrier. My general rule: If I use a
.
in a regex, I always specify (?s) or (?-s) ahead of it, and ignore the. matches newline
box.Of course, it is problem in that posters with questions of these type rarely give enough information to give foolproof solutions.
Also, I’ve noticed that you’re “casual” about the
. matches newline
or(?s)
/(?-s)
specifications in other thread’s postings, when it is NOT as clear cut as this thread’s is. Just a word to “be careful” – this setting matters a lot when it comes to regex. -
As far as I can understand, you take a more protective attitude towards OP than me, maybe experience had taught you that. It’s fine and have nothing against it. However, in my case and in the absence of any compelling reason I try to respond as accurately as possible to the data exemplified, assuming that the posted example is representative of the data set to be processed. In other words, I am confident that OP made a correct analysis of the issue.
Which is the best approach? Neither is unreasonable a priori, nor are wrong those who decided to take a more or less intermediate position. I suppose context would help to make better decisions - see my second example, below.
Concerning the casual point, what I understand - and if I am wrong, please correct me - is that the modifier
(?s)
or its negated form has real significance when it is quantified, i.e. affected by*
or+
. I mean, given this data:Line1 Line2 Line3 Line4
a search expression as
Lin.\d
will get four matches, no matter what adjustment has been made -(?s)
or(?-s)
. In this case it is useless to take precautions. However, if a quantifier is added, either*
or+
, then the regex in one match could find the whole set given the. matches newline
is checked.Thus, in the last regex I posted, https://community.notepad-plus-plus.org/post/52389, in which the data provided was insufficient, I took due precautions to control the regex, changing the modifier as appropriate so that the regex would do the desired job correctly.
Anyway, rest assured that my posts aim to help people, as far as my limited knowledge allows. And if any response of mine is not entirely adequate, it will have been by unintentional error, but never by malice.
-
@astrosofista said in Help find 2nd occurrence and replace:
I am confident that OP made a correct analysis of the issue.
I guess you haven’t read as many of these types of postings as I have.
You’re predicating your subsequent statements on that “correct analysis”. :-)Sure the case of
.
by itself (not in a.*
,.*?
,.+
or.+?
construct, and some others less frequently used) is fairly immune to trouble; for Windows files (which is what we talk about mostly, obviously) a single.
shouldn’t be able to cause line-ending harm (it would take two.
to match the\r\n
).It’s okay, I was just trying to offer some friendly advice concerning
.
. I think it has been beat to death.rest assured that my posts aim to help people,
Of course. I think you’ve been a strong contributor to date.
-
Hi, @astrosofista, @alan-kilborn and All,
Yes, at first sight, we could think that a single regex dot character, between two characters, is handled in a same way, whatever the state of the
s
in-line modifier and/or the state of the. matches newline
optionBut it’s not always the case :-( For instance, let’s suppose the
3
-lines text, below, withUnix
EOL, the last line without any EOL, and a space char between the last twoc
letterscbcc1cc LF
ccZcc9cc LF
cc cThe regex
(?s)c.c
would match, successively, the7
strings cbc , c1c , cLF
c , cZc , c9c , cLF
c and c c, so all the text !Whereas the regex
(?-s)c.c
would match the5
strings cbc , c1c , cZc , c9c and c c, only !
Therefore, it is best to remain precise and indicate the modifiers
(?s)
and/or(?-s)
, whenever necessary ;-)Best Regards,
guy038
-
@guy038 said in Help find 2nd occurrence and replace:
But it’s not always the case :-( For instance, let’s suppose … Unix EOL
Which is exactly why I had said:
for Windows files
:-)
-
@Alan-Kilborn said in Help find 2nd occurrence and replace:
It’s okay, I was just trying to offer some friendly advice concerning …
And I took it like this. What’s more, it’s even good advice, since I only considered the case of Windows, ignoring Unix and Mac.
Thank you, and I’ll keep that in mind.
Have fun!
-
Hi @guy038
Thank you, your’s is good advise, as well as @Alan-Kilborn’s. As I told him, I only took into consideration the Windows case, ignoring other platforms.
Will try to not forget it.
Best Regards