Contextual search syntax
-
The A and X below indicate the severity level of the event log messages in the file I’m using. I’d like to search for Code_123 but only when severity level is X. Is this possible?
Notes: the A and X are always the first character in the line. The number of characters between the severity level and Code_123 are always the same.
A 10-July-2023 13:22:14 Code_123 Software error
X 10-July-2023 15:42:18 Code_123 Software error -
Is this possible?
Yes.
Use Regular Expression mode checkbox in the Find dialog.
FIND =
(?-s)^X.*?\KCode_123
This searches for
(?-s)
= makes sure.
wildcard does not match newlines- you could also just turn off that checkbox, but since a checkbox cannot be copy/pasted, I like using this when I post a regex (regular expression) for others to use
^
= beginning of the lineX
= literal X character.*?
= 0 or more instances of any charcter before the first instance (non-greedy) .- Since you said the number of characters between stays the same, you could also use
.{23}
instead of.*?
to match exactly those 23 characters (if I have counted correctly)
- Since you said the number of characters between stays the same, you could also use
\K
= “reset” the match… it requires everything to the left to be true, but the “real” match starts after the\K
Code_123
= the real text that you want to search for
The useful references below, especially the User Manual link, can tell you more about each of the special symbols used in this regex
----
Useful References
- Please Read Before Posting
- Template for Search/Replace Questions
- Formatting Forum Posts
- Notepad++ Online User Manual: Searching/Regex
- FAQ: Where to find other regular expressions (regex) documentation
----
edit: PS: After posting, I went back and viewed the source of your original post. Apparently, there are multiple spaces and tabs in the gaps between words, so I count 27 characters now.A 10-July-2023 13:22:14 Code_123 Software error X 10-July-2023 15:42:18 Code_123 Software error
So the alternative would actually be
.{27}
. This underscores the importance of using the</>
button when entering data into the Community forum
…as is suggested in the “Template for Search/Replace Questions” and “Formatting Forum Posts”, both of which are referenced from the “Please Read Before Posting” which you may not have read before posting, despite it being the first post you saw just before the New Topic button you clicked to make this post. -
@PeterJones Thank you so much Peter, that was exactly what I was looking for!! And your detailed explanation helped me understand it too!