• Login
Community
  • Login

Help with regular expression.

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
9 Posts 5 Posters 2.2k 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.
  • В
    Веселин Николов
    last edited by Aug 16, 2018, 6:30 PM

    Hi,
    I need help with regular expression, the situation is :

    need to delelete lines like this:
    @0C 73 20 2C
    @0D 5E 00 00
    @12 E0 B1 4D
    …

    All lines that begin with @ and are with same length (12 chars)

    Thanks in advance.

    1 Reply Last reply Reply Quote 1
    • L
      Lena Marokez
      last edited by Aug 16, 2018, 6:33 PM

      Replace with regular expressions
      this line:@[[:alnum:]]{2} [[:alnum:]]{2} [[:alnum:]]{2} [[:alnum:]]{2}
      with blank

      1 Reply Last reply Reply Quote 1
      • В
        Веселин Николов
        last edited by Aug 16, 2018, 8:37 PM

        It works!!! :)

        Thanks.

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by guy038 Aug 16, 2018, 10:18 PM Aug 16, 2018, 10:10 PM

          Hello, @веселин-николов, @lena-marokez, and All,

          As the text to be deleted seems to be a list of four bytes values, in hexadecimal, here is a more strict regex syntax, using the Posix class of characters [[:xdigit:]] :

          SEARCH ^@(?:[[:xdigit:]]{2}\x20?){4}\R

          REPLACE Leave EMPTY

          Notes :

          • The part ^@ searches for a literal @ character, at beginning ( ^ ) of line

          • Then, the part [[:xdigit:]]{2} tries to match two hexadecimal digits, followed the an optional space character ( \x20? )

          • That second part is enclosed in a non-capturing group ( (?:.......) ) and must be present fourth times {4}, ending with its line break ( \R ), which can be, either, \r\n if Windows files, \n if Unix files or \r for Mac files

          • As the replacement field is empty, any entire matched line is, then, deleted

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 2
          • T
            Terry R
            last edited by Terry R Aug 16, 2018, 10:57 PM Aug 16, 2018, 10:56 PM

            Hi @guy038, I’m wondering if your regex is a bit too lenient.

            The user stated the line starts with the ‘@’ and is 12 characters long. Thus the \h or \x20 is a required parameter. I can see you were trying to lump all 4 instances together so you had to make the space an option to cater for the last instance. Your regex will allow for lines such as @12 3456 7A to also be included.

            Without knowing the types of lines that might exist in the data this could potentially include lines that should not be erased. Whilst my idea of the regex is longer winded it builds on yours and is more strict as well. So mine says lets look for 1 instance of a pair of hexadecimal digits, then 3 lots of a ‘space’ followed by a pair of hexadecimal digits.

            Find what: ^@[[:xdigit:]]{2}(?:\x20[[:xdigit:]]{2}){3}\R
            Replace with: Leave EMPTY

            I welcome your feedback.

            Terry

            1 Reply Last reply Reply Quote 2
            • C
              Claudia Frank
              last edited by Claudia Frank Aug 16, 2018, 11:28 PM Aug 16, 2018, 11:27 PM

              May I ask why not just using ^@.{11}$ ?
              Shouldn’t this fullfil the OP request?

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 1
              • T
                Terry R
                last edited by Aug 16, 2018, 11:48 PM

                Hi @Claudia-Frank , I suppose the real question is, how do we best fulfil the OP request. All the regexs supplied would indeed do as requested. Some are more accurate in their aim than others.

                Without knowing the full extent of the data in the files we can never know for sure that we aren’t causing some ‘harm’ to the data. If we try and keep the regex as tight as we can, and I suppose we also provide some caveat (our assumptions) we can be happy that we’ve done our bit.

                Terry

                1 Reply Last reply Reply Quote 3
                • G
                  guy038
                  last edited by guy038 Aug 17, 2018, 9:44 AM Aug 17, 2018, 9:42 AM

                  Hi, @terry-r and All,

                  You’re perfectly right about it ! I did not notice the other possible matches, due to the optional quantifier ? Thus, when I said “… A more strict regex…”, it is, rather, a “… A less strict regex…” :-((

                  Finally, based on the OP needs :

                  All lines that begin with @ and are with same length (12 chars)

                  The Claudia’s solution seems just correct, doesn’t it ?


                  Of course, if no other line, in file contents, begins with the @ character, we could, simply, use the regex S/R :

                  SEARCH ^@.+\R

                  REPLACE Leave EMPTY

                  Cheers,

                  guy038

                  1 Reply Last reply Reply Quote 2
                  • C
                    Claudia Frank
                    last edited by Aug 17, 2018, 11:10 AM

                    That’s the wonderful world of regex - multiple solutions for one problem :-)

                    Cheers
                    Claudia

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