• Login
Community
  • Login

Embedded Parentheses (color coding for quick observation)

Scheduled Pinned Locked Moved General Discussion
3 Posts 3 Posters 879 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J
    Jason W
    last edited by Sep 16, 2021, 4:08 PM

    Looking for a particular use theme: embedded parenthetical groupings with unique color highlights…

    Woudl this be a theme or a plugin? I’d like all parentheses to show up with a unique color for each pairing - so when i have embedded parenthetical groups within other (groups) I can quickly see where the start and end of each group is without having to click onte of the parentheses to start?

    IF 0 ( ( Sequence Number NOT = “” ) AND ( Sequence Number Matches Pattern of “xx.x” ) OR (Color is Black) AND (YouDaMan(Friends+Friends)) )

    1 Reply Last reply Reply Quote 0
    • G
      guy038
      last edited by guy038 Sep 17, 2021, 12:05 PM Sep 16, 2021, 5:07 PM

      Hello, @jason-w and All,

      No need for a theme or plugin ! You can quickly see ranges of characters, with well-balanced number of parentheses, with a recursive regex construction !

      So :

      • Open the Find dialog ( Ctrl + F )

      • SEARCH \((?:[^()]++|(?0))*\)

      • Un-tick all the options

      • Optionally, tick the Wrap around option

      • Select the Regular expression search mode

      • Click on the Find Next button

      => From the caret position, consecutive greastest ranges, even on several lines, of characters, surrounded with parentheses and possibly containing zones with well-balanced parentheses, too, should be matched !

      Just play around with this regex, placing your caret, right before any opening parenthesis and hitting the F3 key


      Second solution :

      • Open the Mark dialog ( Ctrl + M )

      • SEARCH \((?:[^()]++|(?0))*\)

      • Un-tick all the options

      • Optionally, tick the Wrap around

      • Tick the Purge for each search option

      • Select the Regular expression search mode

      • Click on the Mark all button

      => Again, from caret position, consecutive greastest ranges, even on several lines, of characters, surrounded with parentheses and possibly containing zones with well-balanced parentheses, too, should be marked in red !


      Test this recursive regex, against your example :

      IF 0 ( ( Sequence Number NOT = “” ) AND ( Sequence Number Matches Pattern of “xx.x” ) OR (Color is Black) AND (YouDaMan(Friends+Friends)) )
      

      and, for instance, against this other text, on several lines :

      123(456((789)(123)(45
      6()789)123))456(78
      9)123)456)))789(123(456((789)
      (123)456)789)(123((456)789)(123(4
      56(789))(123)4
      56(789))123
      

      Moving, each time, your cursor at some positions, right before an ( parenthesis !

      Best Regards,

      guy038

      P.S. :

      I would like to show you a brief overview of the recursion process, in regular expressions, by taking a concrete example.

      From the second example, above, the first match of the regex \((?:[^()]++|(?0))*\) is the string :

      (456((789)(123)(45
      6()789)123))
      

      that I rewrite as (456((789)(123)(45xy6()789)123)), where x = \x0d and y = \x0a

      Now, let’s rewrite this string by inserting some non-significant spaces, for readability :

      (         456      (   (      789     )   (      123     )   (      45xy6     (                   )      789     )      123       )   )
      

      Keep in mind that the part (?0), in our regex, is a recursive call to the entire regex pattern. In other words, (?0) is strictly equivalent to the regex itself : \((?:[^()]++|(?0))*\)

      So, if we use the free-spacing mode (?x), the regex, normally written as :

      (?x)  \(   (?:  [^()]++ |            (?0)         )*   \)
      

      could also be expressed as :

      (?x)  \(   (?:  [^()]++ |  \((?:[^()]++|(?0))*\)  )*   \)
      

      and so on… for the same results


      Thus, the string (456((789)(123)(45xy6()789)123)) can be found with the following regex elements :

      (         456      (   (      789     )   (      123     )   (      45xy6     (                   )      789     )      123       )   )
      (         ---      ----------------------------------------------------------------------------------------------------------------   )
      \(     [^()]{3}+                                                         (?0)                                                         \)
      

      Which, in turn, can be rewritten as :

      (         456      (   (      789     )   (      123     )   (      45xy6     (                   )      789     )      123       )   )
                ---      (   ----------------   ----------------   -----------------------------------------------------      ---
      \(     [^()]{3} +  \(        (?0)               (?0)                                 (?0)                            [^()]{3}+    \)  \)
      

      Which, again, can be broken down into :

      (         456      (   (      789     )   (      123     )   (      45xy6     (                   )      789     )      123       )   )
                ---                 ---                ---                -----     ---------------------      ---            ---
      \(     [^()]{3}+   \(  \(  [^()]{3}+  \)  \(  [^()]{3}+  \)  \(  [^()]{5}+            (?0)            [^()]{3}+  \)  [^()]{3}+    \)  \)
      

      And, finally, can be expressed as :

      (         456      (   (      789     )   (      123     )   (      45xy6     (                   )      789     )      123       )   )
                ---                 ---                ---                -----         --------------         ---            ---
      \(     [^()]{3}+   \(  \(  [^()]{3}+  \)  \(  [^()]{3}+  \)  \(  [^()]{5}+    \(  ([^()]{1}+){0}  \)  [^()]{3}+  \)  [^()]{3}+    \)  \)
      

      As you can see, no recursive pattern exists anymore in this final regex structure and, using the free-spacing mode (?x), we can create the functional ( and long ! ) regex, below :

      (?x) \( [^()]{3}+ \( \( [^()]{3}+ \) \( [^()]{3}+ \) \( [^()]{5}+ \( ([^()]{1}+){0} \) [^()]{3}+ \) [^()]{3}+ \) \)

      And verify that it does match the string :

      (456((789)(123)(45xy6()789)123))


      So, the regex :

      (?x) \( [^()]{3}+ \( \( [^()]{3}+ \) \( [^()]{3}+ \) \( [^()]{5}+ \( ([^()]{1}+){0} \) [^()]{3}+ \) [^()]{3}+ \) \)

      is just one example of the final non-recursive expression, resulting of the use of the recursive pattern :

      (?x) \( (?: [^()]++ | (?0) )* \), once the recursion process is complete.

      Of course, many other combinations are possible, all expressed by this powerful and short recursive regex :-))

      1 Reply Last reply Reply Quote 4
      • A
        Alan Kilborn
        last edited by Sep 17, 2021, 2:50 PM

        Here’s some related good information: https://community.notepad-plus-plus.org/post/256

        1 Reply Last reply Reply Quote 2
        3 out of 3
        • First post
          3/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors