Generic Regex: Replacing in a specific zone of text
By
guy038
4y ago
Source
community.notepad-plus-plus.orgGeneric Regex: Replacing in a specific zone of textnotepad-plus-plus.orgTwo other examples regarding this generic regex ! In these ones, we’ll even restrict the replacements to each concerned zone before a # character ! Paste the text below in a new tab : 123456 (START) Case_1 This is a (short) text to (easily) see the results (of the modifications)# (12345) test (67890) val (250) 123456 Case_2 And the (obvious) changes occur only in (the) "descrip" tag # Parentheses (Yeaah) OK val (500) 123456 (END) Case_3 All (the) other tags are (just) untouched #(This is) the end (of the test) val (999) In this first example, of single-line tags , two solutions are possible : Use the complete generic regex (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) where ESR = # which leads to the functional S/R : SEARCH (?-s)(?-i:|(?!\A)\G)((?!#).)*?\K(?:\x20\(.+?\)) REPLACE Leave EMPTY => This time, in addition to only replace in each .......... zone, NO replacement will occur after the # character of each tag ! Use the simplified solution and add a ESR condition at the end of the regex, giving this generic variant (?-s)(?-i:BSR|(?!\A)\G).*?\K(?-i:FR)(?=ESR) SEARCH (?-s)(?-i:|(?!\A)\G).*?\K(?:\x20\(.+?\))(?=.*#) REPLACE Leave EMPTY However, this other solution needs that all the tags contains a comment zone with a # char Now, paste this other text below in a new tab : 37--001 This-is -a--very---< # Dashes - - - OK/text> 4-1234 37--002 -small----#---example 9-0012 37--003 -of-a-text- which- 1-9999 37--004 need -to-be- modi fied # but - not - there 0-0000 This second example is a multi-lines replacement, in each ............. zone only and also limited to the part before a # char which can be present or not Of course, we’ll have to use the complete generic regex (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR).)*?\K(?-si:FR) but, instead of a single (?!ESR), we’ll have to use this variant : (?-si:BSR|(?!\A)\G)(?s-i:(?!ESR_1)(?!ESR_2).)*?\K(?-si:FR) So, the functional regex S/R becomes : SEARCH (?-si:|(?!\A)\G)(?s-i:(?! )(?!#).)*?\K-+ REPLACE \x20 => ONLY IF a sequence of dashes is located in a .......... zone AND, moreover, before a possible # char, it will be replaced with a single space character As you can verify, the third multi-lines ............. zone does not contain any # char. Thus, all dash characters, of that tag, are replaced with a single space char ! Remainder : You must use, at least, the v7.9.1 N++ release, so that the \A assertion is correctly handled Move to the very beginning of file, before any Find Next sequence or Replace All operation Do not click on the step-by-step Replace button
You might also wanna read
AI-Generated Code Overwhelms Human Reviewers: Strategies to Streamline Code Review
The article examines how AI-generated code from tools like Claude Code, Copilot, and Cursor is overwhelming traditional human code review pr
hackernoon.com·4h ago
Why CPU-Based Autoscaling Fails for GPU Inference — and How KEDA Fixes It
This article explains why CPU utilization is a poor autoscaling signal for GPU inference workloads, using a real-world incident where an HPA
hackernoon.com·4h ago
Try-Works: An Index of Projects Referencing Recursive Language Models
This is a brief project/product index entry for "Try-Works" that references the MIT paper on Recursive Language Models, which demonstrated a
Fortress: A stealth Chromium engine that modifies browser fingerprints to evade bot detection for scrapers
Fortress is a stealth Chromium engine designed to help scrapers and browser automation agents avoid detection by bot detectors. It modifies
Git Commit Hash Malleability: Attackers Can Forge Verified Signatures Without Access to Signing Keys
This paper demonstrates a critical vulnerability in Git commit signing: an attacker can produce a second, distinct commit with identical tre

10 Tech Trends That Will Define 2026: The Future Is Already Here
BrightCoding·7h ago

Comments
Sign in to join the conversation.
No comments yet. Be the first.