• Login
Community
  • Login

add attribute to element from value of next element

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
2 Posts 2 Posters 175 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.
  • A
    Anar Movsumov
    last edited by Anar Movsumov Mar 19, 2023, 1:28 PM Mar 19, 2023, 1:26 PM

    Hi community! Need your help. I have this code

    <triggers>
      <request>
        <num>1</num>
        <lastname>xxx</lastname>
        <firstname>xxx</firstname>
        <middlename>xxx</middlename>
        <birthday>xxx</birthday>
        <birthplace>xxx</birthplace>
        <doctype>1</doctype>
        <docno>xxx</docno>
        <docdate>xxx</docdate>
        <docplace>xxx</docplace>
        <inn>xxx</inn>
        <pfno>xxx </pfno>
        <reason>xxx</reason>
        <reason_text>xxx </reason_text>
        <consent>xxx</consent>
        <consentdate>xxx</consentdate>
        <consentenddate>xxx</consentenddate>
        <admcode_inform>xxx</admcode_inform>
        <consent_owner>xxx </consent_owner>
        <contractno>xxx </contractno>
      </request>
    ......................... (many requests here)
    </triggers>
    

    and i want it to look like this

    <triggers>
      <request num="1">
        <lastname>xxx</lastname>
        <firstname>xxx</firstname>
    ...
    </request>
    

    so <num>value</num> goes to <request num=“value”>

    any help would be really really appreciated.

    M 1 Reply Last reply Mar 19, 2023, 3:15 PM Reply Quote 0
    • M
      Mark Olson @Anar Movsumov
      last edited by Mark Olson Mar 19, 2023, 3:17 PM Mar 19, 2023, 3:15 PM

      @Anar-Movsumov
      find/replace form; replace (?s)<request>\s*<num>(\d+)</num>(.*?</request>) with <request id="\1">\2.

      The very excellent regex manual on the reference website has some pages that may be of interest:

      • single character matches on \s, ., and \d
      • capture groups and backreferences for why I wrapped (\d+) and (.+</request>) in parentheses
      • search modifiers to understand how (?s) works
      • multiplying operators on why you need to use .*? and not .* to match everything between the <num> element and the remaining children of the <request> tag.
      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