All Topics
All Topics
Technology
Technology
AI
AI
Business
Business
Entertainment
Entertainment
News
News
Programming
Programming
Science
Science
Design
Design
Environment
Environment
Finance
Finance
Crypto
Crypto
Politics
Politics
Sports
Sports
Education
Education
Gaming
Gaming
Art
Art
Music
Music
Health
Health
Security
Security
Books
Books
Food
Food
Travel
Travel
Personal
Personal
Bluesky
Twitter

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.org
Snippet from the RSS feed
Two 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

Comments

Sign in to join the conversation.

No comments yet. Be the first.