• Login
Community
  • Login

finding a partial number with Wildcard

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 427 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.
  • D
    Darren Wartnaby
    last edited by Jun 21, 2023, 2:17 PM

    Hi i have an XML file with multiple account number and i want to Find in the Tag <AccNbr> all lines that have 222 as any part of the number how would I search

    <AcctNbr>111111222</AcctNbr>

    A 1 Reply Last reply Jun 21, 2023, 2:26 PM Reply Quote 0
    • A
      Alan Kilborn @Darren Wartnaby
      last edited by Jun 21, 2023, 2:26 PM

      @Darren-Wartnaby

      Try:

      Find: (?-s)<AcctNbr>.*?222.*?</AcctNbr>
      Search mode: Regular expression

      M 1 Reply Last reply Jun 21, 2023, 3:02 PM Reply Quote 3
      • M
        mkupper @Alan Kilborn
        last edited by Jun 21, 2023, 3:02 PM

        @Alan-Kilborn said in finding a partial number with Wildcard:

        Find: (?-s)<AcctNbr>.*?222.*?</AcctNbr>
        Search mode: Regular expression

        I would lean towards using (?-i)<AcctNbr>\d*222\d*</AcctNbr>
        XML is case sensitive which is why I use (?-i).
        It’s a number and so I use \d* instead of .*? which allows any character. As I don’t use the dot (.) operator I don’t need the (?-s) nor the ? non-greedy modifier.

        @Darren-Wartnaby - A web page that explains the various things shown here is at https://npp-user-manual.org/docs/searching/#regular-expressions

        Alan’s version is better if there is a chance that whatever is between <AcctNbr> and </AcctNbr> has things other other than numeric digits at times.

        1 Reply Last reply Reply Quote 2
        • D
          Darren Wartnaby
          last edited by Jun 21, 2023, 3:24 PM

          @Alan-Kilborn said in finding a partial number with Wildcard:

          *?222

          Alan thanks so if I want to search for two number in that one string do i repeat this part .*?222.

          A 1 Reply Last reply Jun 21, 2023, 4:43 PM Reply Quote 0
          • A
            Alan Kilborn @Darren Wartnaby
            last edited by Jun 21, 2023, 4:43 PM

            @Darren-Wartnaby said in finding a partial number with Wildcard:

            so if I want to search for two number in that one string do i repeat this part .*?222

            You could, but there are some issues with ordering; i.e., you would only find matches in the left-to-right order of the two numbers specified.

            1 Reply Last reply Reply Quote 2
            • M
              Mark Olson
              last edited by Mark Olson Jun 21, 2023, 5:49 PM Jun 21, 2023, 5:48 PM

              Suppose you want to find text in AcctNbr tags containing the numbers 48, 17, and 34 in any order. You could enumerate every possible ordering of those numbers, but that would become increasingly intractable as the number of things to be matched got larger (e.g., there are 24 possible orderings of four distinct things). This is where forward lookahead really shines.

              (?-si)<AcctNbr>(?=(?:(?!</AcctNbr>).)*?17)(?=(?:(?!</AcctNbr>).)*?34)(?=(?:(?!</AcctNbr>).)*?48).*?</AcctNbr>

              Try that out on the below text to see what I mean.

              <AcctNbr>1 2 3 4</AcctNbr>
              <AcctNbr>1 2 23 14</AcctNbr>
              <AcctNbr>27</AcctNbr>
              <AcctNbr>1234</AcctNbr>
              <AcctNbr>34 18</AcctNbr>
              <AcctNbr>48734</AcctNbr>
              <FooNbr>34 17 48</FooNbr>
              <!-- last three should match -->
              <AcctNbr>34 17 48</AcctNbr>
              <AcctNbr>4817 34</AcctNbr>
              <AcctNbr>483417</AcctNbr>
              

              NOTE: this method allows overlap between things to be matched, e.g., 348 would count for both 34 and 48. There is no efficient way to do this without making the regex much nastier.

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