Community
    • Login

    sample config

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    24 Posts 4 Posters 3.6k 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.
    • PeterJonesP
      PeterJones @Justin Lang
      last edited by

      @Justin-Lang ,

      You could have just replied to the previous post rather than creating a whole new topic. That helps discussion flow.

      You also didn’t use the </> button, which means that the forum may have corrupted your data.

      You also didn’t bother pointing out whether that was two example files or just one.

      All in all, you don’t seem like you want to make it easy for us to help you. That will dramatically reduce the help we can give you.

      If I am reading correctly, a file that has a username and password will have a single line with username xxxx password yyyy, no leading or trailing spaces.

      When I tried a Find in Files on two files, with

      • FIND = (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z

      where pw.txt had

      usexrname admin pasxsword cisco
      username blah password blah
      some other text
      

      (so line 2 has username and password)
      and nopw.txt had:

      usernxame blah paxssword blah
      usexrname admin pasxsword cisco
      some other text
      

      (so that lines 1 and 2 were close, but not actual matches).

      then Find in Files yielded:

      Search "(?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z" (1 hit in 1 file of 2 searched)
        C:\Users\peter.jones\Downloads\TempData\nppCommunity\FindInFiles\nopw.txt (1 hit)
      	Line 1: usernxame blah paxssword blah
      

      I cannot guarantee it will work for you (especially if these files are long). But it might be sufficient for your needs.

      Good luck.

      Justin LangJ 2 Replies Last reply Reply Quote 3
      • Justin LangJ
        Justin Lang @PeterJones
        last edited by

        @PeterJones I am so sorry. I am new to this forum and notepad++. I will learn and submit question properly next time. Thanks for your help

        1 Reply Last reply Reply Quote 0
        • Justin LangJ
          Justin Lang @PeterJones
          last edited by

          @PeterJones that worked great. thanks. Would you mind to explain what each of the regex does? I understand ^ indicates the beginning of the line. (.*) is any character and zero or more preceding characters, but don’t know how (?i-s), \A, \R, \S, \z, and (?!) work. Thx

          Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Justin Lang
            last edited by Alan Kilborn

            @Justin-Lang said in sample config:

            explain what each of the regex does?

            Some great info in the user manual on this.
            See https://npp-user-manual.org/docs/searching/
            Scroll down to the section headed “Regular Expressions”.

            1 Reply Last reply Reply Quote 2
            • PeterJonesP
              PeterJones @Justin Lang
              last edited by

              @Justin-Lang said in sample config:

              @PeterJones that worked great. thanks. Would you mind to explain what each of the regex does? I understand ^ indicates the beginning of the line. (.*) is any character and zero or more preceding characters, but don’t know how (?i-s), \A, \R, \S, \z, and (?!) work. Thx

              • (?i-s): this is a search modifier expression which makes it case-insensitive, and turns off dot-matches-newline
              • \A is an anchor which matches the beginning of the file
              • \R is a control character which matches any newline sequence (\n like Linux, \r like old Macs, \r\n like Windows)
              • \S is the character escape sequence which means “anything that is not a space character” (\s being "anything that is a space character)
              • \z is an anchor which matches the end of the file
              • (?!pattern) is an assertion known as a “negative lookahead”, which means that it only matches if what comes next does not match the regex indicated by pattern

              (the links above are all inside the main link that @Alan-Kilborn just posted)

              So my fancy regex says, "ignore upper-vs-lowercase; dot does not match newline sequences; start at the beginning of the file; look for one or more lines that are followed by a line which doesn’t match username XXX password XXX, followed by the end of the file.

              Looking as my logic, this might fail if username XXXX password XXXX is the first line of the file. If that’s a problem for your data, it will take some tweaking.

              ----

              Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

              PeterJonesP 1 Reply Last reply Reply Quote 1
              • PeterJonesP
                PeterJones @PeterJones
                last edited by

                @Justin-Lang ,

                I said,

                this might fail if username XXXX password XXXX is the first line of the file.

                … and that got me curious. So I played around some more.

                • FIND = (?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z

                this seemed to work even if the username line was the first or last line in the file.

                (I am not super great at find-stuff-that-doesn’t-match regex; I can usually get the regex good enough for my needs, but edge cases sometimes escape me)

                • \A(...)+\z => match a file only if every line matches what’s inside the parens
                • ^(?!...).*$\R* => match this line only if the lookahead in the (?!...) isn’t true
                • username \S+ password \S+ => match a sequence that is literal username, space, one or more non-whitespace, space, password, space, one-or-more non-whitespace.

                So, this says match the file, only if every line doesn’t start with username XXXX password YYYYY, where XXXX and YYYY just cannot contain spaces.

                I think this is a better regex, and worked for me to weed out files that include that whole line (even if it’s at the beginning of the file). Hopefully, this will work for you and your fileset.

                PeterJonesP Justin LangJ 2 Replies Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @PeterJones
                  last edited by

                  PS:

                  In case it’s not obvious: when you (or other people) ask us these kind of regex questions, we usually don’t “just know the answer”, we have to figure it out to answer your question. Just about every regex situation is different, and they require much thought and experimentation to get right (or at least, get “good enough”).

                  The regular users here who answer regex questions have just practiced regex, and have learned a set of tokens that we can build to make more complex expressions. And (as is obvious from my answer_s_, plural), sometimes we get it wrong (or at least, close, but not good enough for all situations). We just take the small chunks we know, coupled with the documentation and a willingness to experiment until we get it right, to develop these expressions. Sometimes, that yields a truly generic expression (like here) which can be used in plenty of situations; and sometimes, it’s highly customized to the exact requirements of the original questioner.

                  The best way to learn regex is to practice by doing: try out lots of smaller regex expressions, until you get a better understanding of how those small pieces work; then practice putting those building-blocks together in new and unique ways. The more you practice, the better you get. (Reading old posts here, and trying to solve the problems, then comparing your solution to the “regulars”'s regexes, is a good way to learn.)

                  Good luck.

                  Justin LangJ 1 Reply Last reply Reply Quote 5
                  • guy038G
                    guy038
                    last edited by guy038

                    Hello, @justin-lang, @alan-kilborn, @peterjones, and All,

                    Here is a second solution which uses the backtraking control verbs (*SKIP) and (*FAIL) or (*F)

                    I assume that the goal is to list all file(s) that do not contain any entire line username •••• password ••••, whatever its case, when using :

                    • The Find All in Current Document button, in the Find dialog

                    • The Find All in All Opened Documents button, in the Find dialog

                    • The Find All button, in the Find in Files dialog


                    SEARCH \A(?s)(.*(?i)^username \S+ password \S+$(*SKIP)(*F)|.+?$)

                    How this regex works ?

                    • First note that there are two alternatives, separated with the | symbol

                      • A) \A(?s).*(?i)^username \S+ password \S+$(*SKIP)(*F)

                        • The part A tries to match any text from the very beginning ( \A ) of current file, till a entire line username •••• password ••••, whatever its case, ( (?i) )
                      • B) \A(?s).+?$

                        • The part B matches any text from the very beginning of current file ( \A ), till the first non-empty line,
                          included ( (?s).+?$ )
                    • So, from the very beginning of file, the regex engine begins to test the first alternative and tries to match an entire line username •••• password ••••

                      • If such a line can be found, then :

                        • The (SKIP) control verb, skips all this match and the working location is moved to right after the line username •••• password ••••.

                        • The second control verb (*F) forces the regex engine to cancel this succesful match

                        • So, the regex engine tries the second alternative \A(?s).+?$, which also fails, as the current location is not, anymore, at the very beginning of current file !

                        • As no other alternative exists, the overall match attempt fails and no match has been found

                      • If, from the very beginning, any text, ending with the entire line username •••• password ••••, cannot be found, further on, in current file, the first alternative is left and the regex engine tries the second alternative… which always succeeds :

                        • The \A assertion is verified as the current location is still the very beginning of current file

                        • And the part (?s).+?$ simply matches any text, from the very beginning, till the end of the first non-empty line


                    Remark :

                    • In order to test this regex against individual files, tick the Wrap around option and click on the Find Next button

                    Best Regards,

                    guy038

                    Justin LangJ 1 Reply Last reply Reply Quote 2
                    • guy038G
                      guy038
                      last edited by guy038

                      Hi, @peterjones, @justin-lang, @alan-kilborn and All,

                      Peter, I’m terribly sorry but I cannot get your regex (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z to work properly :-((


                      I’m using seven files from G1.txt to G7.txt with N++ v7.9.2

                      • Contents of G1.txt, with the concerned line username •••• password •••• as line 1 :
                      username blah password blah
                      usexrname admin pasxsword cisco
                      some other text
                      
                      • Contents of G2.txt, with the concerned line username •••• password •••• as line 2 :
                      usexrname admin pasxsword cisco
                      username blah password blah
                      some other text
                      
                      • Contents of G3.txt, with the concerned line username •••• password •••• as line 3, without EOL chars
                      usexrname admin pasxsword cisco
                      some other text
                      username blah password blah
                      
                      • Contents of G4.txt, with a first blank line and the concerned line username •••• password •••• as line 3
                      
                      usexrname admin pasxsword cisco
                      username blah password blah
                      some other text
                      
                      • Contents of G5.txt, without any line username •••• password •••• :
                      usernxame blah paxssword blah
                      usexrname admin pasxsword cisco
                      some other text
                      
                      • Contents of G6.txt, with a first blank line and without any line username •••• password •••• :
                      
                      usernxame blah paxssword blah
                      usexrname admin pasxsword cisco
                      some other text
                      
                      • Contents of G7.txt, with the concerned text username •••• password ••••, surrounded by digits ( so NOT entire line )
                      usexrname admin pasxsword cisco
                      12345 username blah password blah 67890
                      some other text
                      
                      • If I use my regex \A(?s)(.*(?i)^username \S+ password \S+$(*SKIP)(*F)|.+?$), I do find results which seem correct as the regex outputs, with filter G*.txt :

                        • G5.txt and G6.txt which do not contain any entire line username •••• password ••••

                        • G7.txt which contains text username •••• password •••• but surrounded with numbers

                      48a38c46-4450-413c-b08a-5f713c5541f0-image.png


                      • If I use your regex (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z, I do not find any result !?

                      30158fca-5cb9-4d0b-b78b-3d7c4a0a3e91-image.png

                      Best Regards,

                      guy038

                      PeterJonesP 1 Reply Last reply Reply Quote 0
                      • PeterJonesP
                        PeterJones @guy038
                        last edited by

                        @guy038 said in sample config:

                        I’m terribly sorry but I cannot get your regex (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z to work properly :-((

                        Like I said, that one had bad logic – eating up a whole line before comparing against the unwanted line. Though I’m not sure why you didn’t get any… When I try it with my bad regex, I matched some of the files correctly, but my pw.txt and your g1.txt were incorrectly matched, because they had the username/password line as the first line:
                        9ca07fd6-cda0-44d8-8a0f-b0c6145500d9-image.png

                        Compare to when I switch to my modified/corrected (?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z regex:
                        f4704910-2919-4d2b-b66a-5cb1b46a4471-image.png

                        Notepad++ v7.9.4   (64-bit)
                        Build time : Mar 15 2021 - 01:03:10
                        Path : C:\usr\local\apps\notepad++\notepad++.exe
                        Admin mode : OFF
                        Local Conf mode : ON
                        OS Name : Windows 10 Enterprise (64-bit) 
                        OS Version : 1903
                        OS Build : 18362.1256
                        Current ANSI codepage : 1252
                        Plugins : ComparePlugin.dll ExtSettings.dll HexEditor.dll LuaScript.dll MarkdownViewerPlusPlus.dll mimeTools.dll NppConsole.dll NppConverter.dll NppEditorConfig.dll NppExec.dll NppExport.dll NppFTP.dll NppUISpy.dll PreviewHTML.dll PythonScript.dll QuickText.dll TagLEET.dll XMLTools.dll 
                        
                        1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by guy038

                          Hi, @peterjones,

                          I still don’t understand why I do not get any results with your first version (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z !?

                          • Could it be related to enhancements in Npp v7.9.3 or v7.9.4 releases ( forbidden with XP machines )

                          • Could it be related to the update of Scintilla ?

                          • I don’t think that it happens because of Win XP itself ! Could you make a quick test to verify your first regex version with N++ v7.9.2. Thanks !


                          Fortunately, your second version (?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z works nicely, as the picture, below, shows and finds the same files as my own regex. Pretty reassuring, isn’t it ?

                          fd941129-e704-49f4-a905-c793528b4ec7-image.png

                          BR

                          guy038

                          PeterJonesP 1 Reply Last reply Reply Quote 0
                          • PeterJonesP
                            PeterJones @guy038
                            last edited by

                            @guy038 said in sample config:

                            Could it be related to enhancements in Npp v7.9.3 or v7.9.4 releases

                            I am getting strange results. The first time I tried in v7.9.2, it didn’t work, and I thought I confirmed that 7.9.2 and 7.9.3 didn’t work. But then I went back to v7.9.2 and tried my new one followed by my old one, and now it seems that the old regex is working in v7.9.2:

                            434db432-416e-4555-8c71-a79c6eee86be-image.png

                            It’s not truly important. The fixed regex works consistently for me and for you.

                            PeterJonesP 1 Reply Last reply Reply Quote 1
                            • PeterJonesP
                              PeterJones @PeterJones
                              last edited by

                              @Justin-Lang,

                              Sorry, we got into the weeds. In case it’s not clear to you: Don’t use my first regex; there are too many problems with it.

                              Instead, use my second, which works more reliably: (?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z.

                              Also, the \A syntax won’t work correctly if you are using a Notepad++ older than v7.9.1, so make sure you have a recent Notepad++. v7.9.1 was the last to have auto-update triggered, so if you’ve checked for updates, you should be on at least v7.9.1. I did confirm that v7.9.1 works with my second regex

                              1 Reply Last reply Reply Quote 1
                              • guy038G
                                guy038
                                last edited by guy038

                                Hi, @peterjones,

                                I did some other tests and there’s definitively something strange with your first regex version. Like you, I got a positive result, just once and some subsequent tests did not find anything !!! Right now, I’m still unable to see the right point and I’m still investigating :-((


                                Now, regarding your second version, sorry, but I’ve found out an issue when a correct line username •••• password •••• begins a line and is followed with some characters, as in the G8.txt file, below :

                                usexrname admin pasxsword cisco
                                username blah password blah 67890
                                some other text
                                

                                With your second regex version (?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z, the G8.txt file is not listed in the Search results panel, as it should be !

                                If I slightly change your regex as (?i-s)\A(^(?!username \S+ password \S+$).*\R?)+\z, this time, the G8.txt file is correctly part of the list

                                As you can see, I put a $ assertion at the end of the negative look-ahead, deleted this same assertion previously located before \R*, which I also modified as \R? as any line has EOL chars, only once or possibly not in case of the last line of the file !

                                The path is difficult but we will get there !

                                Best Regards

                                guy038

                                PeterJonesP 1 Reply Last reply Reply Quote 1
                                • PeterJonesP
                                  PeterJones @guy038
                                  last edited by

                                  @guy038 said in sample config:

                                  As you can see, I put a $ assertion at the end of the negative look-ahead,

                                  Yes, that definitely makes more sense, from the logical/algorithmic standpoint. @Justin-Lang should use that variant if using my “family” of regex at all. Of course, this all assumes that the original assumptions I made, based on just one piece of example text, is sufficient to thoroughly describe the exact rules. The OP has never confirmed whether spaces are allowed before or after, or whether it should match or not match in the weird situations like text before and/or after on the same line, or not. So at this point, I think I’ve done enough for this question from the OP.

                                  1 Reply Last reply Reply Quote 0
                                  • Justin LangJ
                                    Justin Lang @PeterJones
                                    last edited by

                                    @PeterJones thank you so much for providing well explainnation of the regex. I am learning how to use it to match pattern in the network configuration files. Regex is very interesting and useful to quickly find matches in the file but very confusion too. Thanks for your help. I will continue to rely on your help as I am learning regex if you don’t mind.

                                    1 Reply Last reply Reply Quote 0
                                    • Justin LangJ
                                      Justin Lang @PeterJones
                                      last edited by

                                      @PeterJones thank you for your advice. I will continue try and learn regex. There is some regex tester online, but each of them support its own flavor of regex. Do you know which regex online tester that support .NET regex ? thanks

                                      PeterJonesP 1 Reply Last reply Reply Quote 0
                                      • PeterJonesP
                                        PeterJones @Justin Lang
                                        last edited by

                                        @Justin-Lang ,

                                        which regex online tester that support .NET regex ?

                                        The Regular Expression FAQ has links to external regex testers. One or more of them may support .NET regex, but I don’t know off the top of my head. Notepad++ uses the Boost regex engine (which is a variant of PCRE), not .NET regex. Since this forum is focused on discussing Notepad++, not supporting and answering questions on all flavors of regex, so if you have .NET regex specific questions, here is not the right place for them.

                                        1 Reply Last reply Reply Quote 0
                                        • Justin LangJ
                                          Justin Lang @guy038
                                          last edited by

                                          @guy038 thanks so much.

                                          1 Reply Last reply Reply Quote 1
                                          • guy038G
                                            guy038
                                            last edited by guy038

                                            Hello, @peterjones, @justin-lang, @alan-kilborn and All,

                                            To @peterjones only :

                                            Let’s back to your first regex version (?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z. This regex look for a continuous range of lines, not followed with an entire line username •••• password ••••, between the very beginning and the very end of current file. In this case a match occurs and this file is displayed in the Search results window

                                            But, if so, in order to match the \z assertion, as the last line of current file may not end with EOL chars, this first version should be (?i-s)\A(^.*\R?(?!^username \S+ password \S+$))+\z with a question mark ? after \R ?

                                            And, indeed, this first modified version does work on my machine, with the additional results already known when the username •••• password •••• is the first line of current file

                                            Well, end of the story regarding this bugged version, anyway !!


                                            To All :

                                            I improved my own version to :

                                            SEARCH \A(?s)(?:.*?(?i)^username \S+ password \S+$(*SKIP)(*F)|)

                                            As you can see :

                                            • The regex now uses a non-capturing group ( could be interesting if file of big size or with long lines ! )

                                            • It searches for the smallest range of chars till a entire line username •••• password ••••

                                            • In case of no line username •••• password •••• exists, in current file, the second empty alternative simply searches the \A position, which is the very beginning location of current scanned file !


                                            Now, here is an off-topic subject :

                                            When doing all my tests, I wanted to keep the “Find in Files” dialog opened, after each click on the Find All button ! So, I changed, in the <FindHistory •••••••• > section of the config.xml file, the value of the dlgAlwaysVisible attribute from no to yes

                                            However, after re-starting N++, no luck : the “Find in Files” dialog was still closed after running a search :-(( I verified that this wrong behavior occurs since the v7.9.0 release !

                                            Could you confirm me that fact, with the new Non-XP releases ? In case of positive answer(s), I will create an issue, on GitHub !

                                            Best Regards,

                                            guy038

                                            Alan KilbornA 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors