Find text and replace at the top of file
Is there a way to find text, in this case <!DOCTYPE HTML> in all JSP files and replace it at the top of the file. Like, cut and paste at the top. If this is not possible, I would like to find all files which doesn’t have <!DOCTYPE HTML> at the beginning.
@Alan-Kilborn Thanks for your response. What would be the regular expression to find in files which doesn’t have <!DOCTYPE HTML> at the top of the file.
For that, I’d be tempted to try
Note that this one matches only a single character when the special string is NOT in a file.
But my eyes are swimming…the usage of this sequence
?<!versus this one
?!<!meaning two totally different and unrelated things. :-)
@Alan-Kilborn Seems like (?<!\x0A)^(?!<!DOCTYPE HTML>) is working. Thanks for your help.
@Alan-Kilborn One more thing I found is (?<!\x0A)^(?!<!DOCTYPE HTML>) is listing down the files which doesn’t have <!DOCTYPE HTML> at all in the file. I don’t need those files. I need only the files which contains <!DOCTYPE HTML> but not at the top.
Okay, okay, one more freebie, but after that unless I start charging your for consulting, the others here (and really, me, too) are gonna get all over me/us. :-)
(?<!\x0A)^(?!<!DOCTYPE HTML>)(?s).*?<!DOCTYPE HTML>
NOW, What you owe ME is to interpret this and reply back with how it works (assuming it does)!
I do not get much credit for intervening at the end of the discussion to announce that the negative look-ahead
(?!<!DOCTYPE HTML>)sems useless, in the last version of the regex !
Indeed, this shorter syntax
(?<!\x0A)^(?s).+<!DOCTYPE HTML>is enough to get the job done ;-)) As the former regex, it even detects files whose the first line is, let’s say, as
ABC<!DOCTYPE HTML>( so, not exactly at the very beginning of current file ) !
True enough, although these discussions tend to evolve to “build up a regex” by just adding on to what was said last. That’s what I did in this case.
Throwing most of it out might have been more confusing to the OP.
But I’d still like the OP to answer my question as well as now a second question: Why the
+in @guy038 's regex is the absolute key to the whole thing!