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
-
@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”. -
@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 bypattern
(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 makeregex 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 usingCtrl+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. -
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 trueusername \S+ password \S+
=> match a sequence that is literalusername
, 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.
- FIND =
-
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.
-
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 theFind 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 lineusername •••• password ••••
, whatever its case, ((?i)
)
- The part A tries to match any text from the very beginning (
-
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).+?$
)
- The part B matches any text from the very beginning of current file (
-
-
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 lineusername •••• 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 thefirst
non-empty line
-
-
Remark :
- In order to test this regex against individual files, tick the
Wrap around
option and click on theFind Next
button
Best Regards,
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
toG7.txt
with N++v7.9.2
- Contents of
G1.txt
, with the concerned lineusername •••• password ••••
as line1
:
username blah password blah usexrname admin pasxsword cisco some other text
- Contents of
G2.txt
, with the concerned lineusername •••• password ••••
as line2
:
usexrname admin pasxsword cisco username blah password blah some other text
- Contents of
G3.txt
, with the concerned lineusername •••• password ••••
as line3
, 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 lineusername •••• password ••••
as line3
usexrname admin pasxsword cisco username blah password blah some other text
- Contents of
G5.txt
, without any lineusername •••• password ••••
:
usernxame blah paxssword blah usexrname admin pasxsword cisco some other text
- Contents of
G6.txt
, with a first blank line and without any lineusername •••• password ••••
:
usernxame blah paxssword blah usexrname admin pasxsword cisco some other text
- Contents of
G7.txt
, with the concerned textusername •••• 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 filterG*.txt
:-
G5.txt
andG6.txt
which do not contain any entire lineusername •••• password ••••
-
G7.txt
which contains textusername •••• password ••••
but surrounded with numbers
-
- If I use your regex
(?i-s)\A(^.*\R(?!^username \S+ password \S+$))+\z
, I do not find any result !?
Best Regards,
guy038
- Contents of
-
@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 yourg1.txt
were incorrectly matched, because they had the username/password line as the first line:
Compare to when I switch to my modified/corrected
(?i-s)\A(^(?!username \S+ password \S+).*$\R*)+\z
regex:
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
-
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
orv7.9.4
releases ( forbidden withXP
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 ?BR
guy038
-
-
@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:
It’s not truly important. The fixed regex works consistently for me and for you.
-
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 -
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 theG8.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
, theG8.txt
file is not listed in theSearch results
panel, as it should be !If I slightly change your regex as
(?i-s)\A(^(?!username \S+ password \S+$).*\R?)+\z
, this time, theG8.txt
file is correctly part of the listAs 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
-
@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.
-
@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.
-
@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
-
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.
-
@guy038 thanks so much.
-
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 lineusername •••• password ••••
, between the very beginning and the very end of current file. In this case a match occurs and this file is displayed in theSearch results
windowBut, 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 fileWell, 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 theconfig.xml
file, the value of thedlgAlwaysVisible
attribute fromno
toyes
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
-
-
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
Maybe what you are trying to do manually is conflicting with this setting?:
EDIT: I have no idea why my screenshot is twice as big vertically as intended! :-)
-
Hi, @alan-kilborn and All,
How silly am I !! I did not notice that this old setting has been adding in the
Preferences
dialog. Nice ! So , no need to create any issue : good ;-)) This also explains why this pseudo-issue begins with thev7.9.0
issue !After ticking this option, in the
Preferences
dialog and closing N++, the value of thedlgAlwaysVisible
attribute is stillno
in theconfig.xml
file !Cheers,
guy038