Menu

Regular Expression Tester

CFrank
2016-03-18
2016-03-19
  • CFrank

    CFrank - 2016-03-18

    Hello,
    script needs to use both npp views, 2nd is were you type the regular expression.
    Code commented

    import re                                                                                   # import regular expression module
    
    editor1.indicSetStyle(10,INDICATORSTYLE.CONTAINER)                                          # used to color whole match - odd lines
    editor1.indicSetFore(10,(95,215,184))                                                       # the color
    editor1.indicSetAlpha(10,55)                                                                # alpha settings
    editor1.indicSetOutlineAlpha(8,255)                                                         # outlining
    editor1.indicSetUnder(10,True)                                                              # draw under the text
    
    editor1.indicSetStyle(9,INDICATORSTYLE.CONTAINER)                                           # used to color whole match - even lines
    editor1.indicSetFore(9,(195,215,184))
    editor1.indicSetAlpha(9,55)
    editor1.indicSetOutlineAlpha(8,255)
    editor1.indicSetUnder(9,True) 
    
    editor1.indicSetStyle(8,INDICATORSTYLE.ROUNDBOX)                                            # used for sub matches
    editor1.indicSetFore(8,(100,215,100))
    editor1.indicSetAlpha(8,55)
    editor1.indicSetOutlineAlpha(8,255)
    editor1.indicSetUnder(8,True) 
    
    isOdd = False                                                                               # used as even/odd line identifier
    
    def match_found(m):
    
        global isOdd                                                                            # global, because we modify it
        if m.lastindex > 0:                                                                     # >0 = how many submatches do we have
            for i in range(0, m.lastindex + 1):                                                 # loop over it
                if i == 0:                                                                      # match 0 is always the whole match
                    editor1.setIndicatorCurrent(9 if isOdd else 10)                             # set indicator for whole match
                    editor1.indicatorFillRange(m.span(0)[0], m.span(0)[1] - m.span(0)[0])       # draw indicator
                    isOdd = False if isOdd else True                                            # set even/odd identifier - next whole match gets coloured different
                else:
                    editor1.setIndicatorCurrent(8)                                              # set indicator for sub matches
                    editor1.indicatorFillRange(m.span(i)[0], m.span(i)[1] - m.span(i)[0])       # draw it
    
        else:                                                                                   # no sub matches    
            editor1.setIndicatorCurrent(8)                                                      # set the same indicator as normally used in sub matches
            editor1.indicatorFillRange(m.span(0)[0], m.span(0)[1] - m.span(0)[0])               # guess what :-) yes, draw it
    
    def clear_indicator():                                                                      # clear all indicators by
        length = editor1.getTextLength()                                                        # calculating length of document
        for i in range(8,11):                                                                   # and looping over
            editor1.setIndicatorCurrent(i)                                                      # each indicator to
            editor1.indicatorClearRange(0,length)                                               # clear the range
    
    def regex():                                                                                # here the regex starts
    
        clear_indicator()                                                                       # first have a clear view ;-)
    
        pattern = editor2.getLine(0).rstrip()                                                   # next, get the pattern for the second view and cut of line endings
    
        try:                                                                                    # try it
            if editor2.getLine(2)[22:23] == 'I':                                                # is it a case insensitive search?
                editor1.research(pattern, match_found, re.IGNORECASE)                           # then call research with the ignore case flag
            else:                                                                               # otherwise
                editor1.research(pattern, match_found)                                          # call without flag
        except:
            pass                                                                                # is needed to catch incorrect regular expressions
    
    def RegexTester_CHARADDED(args):                                                            # callback which gets called each time when char is added in editor
        regex()                                                                                 # calls itself regex function
    
    def RegexTester_UPDATEUI(args):                                                             # callback gets called and emulates a CHARDELETE notification
        if args['updated'] == 3:                                                                # is a bit of a hack but
            regex()                                                                             # seems to work
    
    if editor2.getProperty('RegexTester_running') != '1':                                       # if the script isn't currently running
        editor.callback(RegexTester_CHARADDED, [SCINTILLANOTIFICATION.CHARADDED])               # register the callbacks charadd
        editor.callback(RegexTester_UPDATEUI, [SCINTILLANOTIFICATION.UPDATEUI])                 # and emulated chardelete
        if editor2.getProperty('RegexTester_running') == '0':                                   # this checks if script was already running, stopped and restarted again
            editor2.replace('RegexTester inActive', 'RegexTester isActive')                     # add the status info to second view
        else:                                                                                   # no, this is the first time we run the script so
            editor2.appendText('\r\n\r\nRegexTester isActive [i] i=sensitive, I=insesitive')    # add the status info to second view
        editor2.setProperty('RegexTester_running', '1')                                         # and set the running identifier
        editor2.setFocus(True)                                                                  # give the second view the focus
        editor2.gotoLine(0)                                                                     # and jump to line 1
    
    else:                                                                                       # the script runs already so this call is used to
        editor.clearCallbacks([SCINTILLANOTIFICATION.CHARADDED])                                # clear the callback charadded and
        editor.clearCallbacks([SCINTILLANOTIFICATION.UPDATEUI])                                 # emulated chardeleted
        editor2.setProperty('RegexTester_running', '0')                                         # set info that script isn't running
        editor2.replace('RegexTester isActive', 'RegexTester inActive')                         # add the status info to second view
        clear_indicator()                                                                       # clear all indicators
        editor1.setFocus(True)                                                                  # and give first view the focus. Have fun 
    

    Cheers
    Claudia

     

    Last edit: CFrank 2016-03-19
  • Dave Brotherstone

    Wow! This is really really awesome!

    demo

    Think re import should be import re as the first line.

     
  • CFrank

    CFrank - 2016-03-19

    Hi Dave,
    of course you are correct (and corrected) but I don't know why/how this happens.
    I've posted the same on npp community which is correct. Hmmm - must be sleepwalking me. ;-)

    Cheers
    Claudia

     
  • Marc Pechaud

    Marc Pechaud - 2017-03-01

    As Python Script ? Not working...

     
  • CFrank

    CFrank - 2017-03-01

    What is not working?
    Do you have any error in the console?

    Cheers
    Claudia

     
  • Marc Pechaud

    Marc Pechaud - 2017-03-02

    Is the script run from Python Script folder as other one?
    I didn't see any console appear, seems to not run when launch it...
    Cheers

     
    • CFrank

      CFrank - 2017-03-02

      Is the script run from Python Script folder as other one?

      What do you mean from folder?
      You start npp and then you goto plugins->python script->new script,
      give it a meaningful name and put the code into it. save it.
      Open both views and run the script by using plugins->python script->scripts->YOUR_NAME -> done
      The animate gif Dave postet should explain its uasage pretty well.

      Concerning the console, Plugins->Python Script->Show Console.

      Cheers
      Claudia

       
  • Marc Pechaud

    Marc Pechaud - 2017-03-03

    I did not make the second view, I see that work, but now all text is marked ;)

     
  • CFrank

    CFrank - 2017-03-03

    Just use an empty line and coloring should go away ;-)

    Cheers
    Claudia

     
  • Marc Pechaud

    Marc Pechaud - 2017-03-03

    Ok, thank you, this is the most interactive code I ever see on NPP, magic !

     
  • Moon

    Moon - 2017-07-27

    Marvellous,inspirational, useful - thanks!

     
  • Preethi Shetty

    Preethi Shetty - 2018-08-23
    Post awaiting moderation.

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.