• Login
Community
  • Login

help! regular express

Scheduled Pinned Locked Moved General Discussion
2 Posts 2 Posters 226 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 Mar 2, 2024, 6:22 AM

    Hello, I want to add brackets around multi-line text

    before
    some text
    some text
    some text
    …

    after
    {
    some text
    some text
    some text
    …
    }

    1 Reply Last reply Reply Quote 0
    • G
      guy038
      last edited by Mar 2, 2024, 11:32 AM

      Hello, @李唯任 and All,

      Not difficult with regular expressions !


      • Do a normal selection of your first text part to modify

      • Open the Replace dialog ( Ctrl + H )

      • Untick all box options

      • Tick the Wrap around box option

      • Select the Regular expression search mode

      • Enter (?-s)(?:.+(\R))+.+ in the Find what: zone

      • Enter {\1$0\1}, in the Replace with: zone

      • FIRST possibility :

        • Tick the In selection box option

        • Click on the Replace All button

      • SECOND possibility :

        • Do not tick the In selection box option

        • Click on the Find Next button

        • Click on the Replace button if the present selection must be replaced (A)

        • Click again on the Find Next button, if the present selection must stay unchanged

        • Return to line (A)

        • And so on…

      => the FIRST possibility just produce one global replacement on your selection whereas the SECOND possibility allows you to choose the zones of text to modify or not !


      Notes :

      • First, the in-line modifier ( (?-s) ) means that the regex character . represents a unique standard char only ( not EOL )

      • Then the (?:.+(\R))+ part represents a bunch of, at least, 1 complete line(s), surrounded by a non-capturing group ( (?:.......) ), each with :

        • 1 or more standard characters

        • Any kind of End of Line ( \R ), so \r\n or \n or \r, stored as the group 1 , which will be re-used in replacement

      • Finally the .+ part match a LAST non-empty line ( Thus, the case if your last section is at the very end of file, without any EOL, will be matched, too )

      • In replacement, we simply write the { character first, followed with an EOL character ( the group \1 ), followed with the whole regex ( $0 ) and, again, followed with a second EOL character and, finally, the } character

      Best Regards,

      guy038

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