Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I replace this text with regex in Notepad++?

In my file I have a lot of:

<WYYYYMMDD>
</WYYYYMMDD>

I want to find / replace them:

<Meeting Week=“WYYYYMMDD”>
</Meeting>

YYYYMMDD are dates, like 20220108.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can I do this with Find Replace in Notepad++?

Example:

<W20220805>
</W20220805>

W never changes.

>Solution :

  • Ctrl+H
  • Find what: <(W\d{8})>(\R)</\1>
  • Replace with: <Meeting Week=“$1”>$2</meeting>
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • Replace all

Explanation:

<               # literally <
(W\d{8})        # group 1, W && 8 digits
>               # literally >
(\R)            # group 2, any kind of linebreak
</\1>           # </  & backreference to group 1 & >

Replacement:

<Meeting Week=“$1”> # new tag with week value = content of group 1
$2                  # same linebreak
</meeting>          # end tag

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading