Community
    • Login

    How to correctly use the regex FREE-SPACING mode feature

    Scheduled Pinned Locked Moved General Discussion
    1 Posts 1 Posters 322 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.
    • guy038G
      guy038
      last edited by guy038

      Hello, All,

      When you use the regex free-spacing mode, with the modifier (?x), or with a (?x....) syntax, like, for instance, (?xs-i) :

      • The regex contents can be written :

        • On a single line, possibly followed with a comment

        • On several consecutive lines, each of witch may be followed by a comment

      • Any usual space character is NOT part of the overall regex

      • Any text located after a first # character are considered as comments and is not part of the overall regex, too

      Thus, you must respect these two rules :

      • Any literal space character, to search for, must be represented with one of these three syntaxes, below :

        • An anti-slash char \ right before that specific space char

        • The [ ] syntax, that is to say a space char between square brackets, representing a character class feature

        • The escape syntaxes \x20 or \x{20} or \x{0020}

      • Any literal sharp character #, to search for, must be represented with one of the three syntaxes, below :

        • An anti-slash char \ right before that specific # char ( => \# )

        • The [#] syntax, that is to say a sharp char between square brackets, representing a character class feature

        • The escape syntaxes \x23 or \x{23} or \x{0023}

      For instance, let’s imagine that you want to match three space chars, surrounded by # characters, with a regex expression, you have the choice between all these syntaxes :

      
      - WITHOUT the FREE-SPACING mode :
      
      #   #
      
      #\x20{3}#
      
      #[ ][ ][ ]#
      
      - WITH the FREE-SPACING mode :
      
      (?x)  \x23\ \ \ \x23      # ESCAPED SPACE char and HEXADECIMAL ESCAPE of #
      
      (?x)  \#[ ][ ][ ]\#       # ESCAPED SHARP char and SPACE in a CHARACTER CLASS
      
      (?x)  [#]\x20\x20{2}[#]   # SHARP char in a CHARACTER CLASS and HEXADECIMAL ESCAPE of SPACE chars
      
      

      IMPORTANT note :

      The multi-lines free-spacing regexes, like, for example :

      (?x-i)
      ^ \x20 \x20 abc  #  String 'abc' with LEADING spaces
      |                #  OR
      ^ \x20 \x20 def  #  String 'def' with LEADING spaces
      

      Works correctly, ONLY IF you click on :

      • The Find Next or the Count button of the Find dialog

      • The Replace or the Replace All button of the Replace dialog

      • The Mark All button of the Mark dialog

      It will fail if you click on :

      • The Find All in Current document button, of the Find diaog

      • The Find All in All Opened Documents button, of the Find dialog

      • The Find All button, of the Find in Files dialog

      • The Find All button, of the Find in Projects dialog

      In addition, it wrongly switches the search mode to the Extended mode


      Instead, when using these latter buttons, you’ll have to use, either :

      • A free-spacing regex, on a single line, ONLY, as, for example, the regex (?x-i) ^ \x20 \x20 abc | ^ \x20 \x20 def

      • The simple regex (?-i)^\x20\x20abc|^\x20\x20def, with all text attached

      Best Regards,

      guy038

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