• Login
Community
  • Login

Contextual search syntax

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 2 Posters 680 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.
  • M
    Mike P
    last edited by Jul 13, 2023, 5:43 PM

    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

    P 1 Reply Last reply Jul 13, 2023, 6:26 PM Reply Quote 0
    • P
      PeterJones @Mike P
      last edited by PeterJones Jul 13, 2023, 6:32 PM Jul 13, 2023, 6:26 PM

      @Mike-P

      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 line
      • X = 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)
      • \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.

      M 1 Reply Last reply Jul 14, 2023, 12:15 PM Reply Quote 6
      • M
        Mike P @PeterJones
        last edited by Jul 14, 2023, 12:15 PM

        @PeterJones Thank you so much Peter, that was exactly what I was looking for!! And your detailed explanation helped me understand it too!

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