Community
    • Login

    Adding parentheses around xml tags.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 2 Posters 606 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.
    • Richard HowardR
      Richard Howard
      last edited by

      Greetings.
      I have a need to add wrapping parentheses around "xref" tagging in my xml based technical manuals.
      It’s a little complicated because some of the xrefs already have the parens, and some don’t need them.
      However, I think the xref tags that need the parens are all preceded by a blank space.
      Here are some examples.
      This is a case where the parens are not needed:

      <ref-setup-item><xref wpid="M50026"/></ref-setup-item>
      

      Here is a case where the parens already exist:

      <para>Inspect cable identification band (<xref
      figid="T5A088-M8T01-036fig1"/>, Item 2).</para>
      

      Here is an example of where they need to be added:

      <item>Replace or repair connector <xref wpid="M50026"/></item>
      

      As you can see in these examples, the only one with a leading blank space is the case where I need to add parentheses.

      Here is what the desired coding structure should look like:

      <item>Replace or repair connector (<xref wpid="M50026"/>)</item>
      

      So, the search would be for xrefs with a leading space. The open paren would go after the space and the closing paren would go after the xref tag closure /> .

      Looking for a regex to handle this across many files.
      Thanks.

      PeterJonesP 1 Reply Last reply Reply Quote 2
      • PeterJonesP
        PeterJones @Richard Howard
        last edited by

        @Richard-Howard ,

        Thank you for the before and after data, and giving examples of lines that shouldn’t change and lines that should. That makes it easier.

        Given the data

        not needed:
        <ref-setup-item><xref wpid="M50026"/></ref-setup-item>
        
        not needed:
        <para>Inspect cable identification band (<xref
        figid="T5A088-M8T01-036fig1"/>, Item 2).</para>
        
        needed:
        <item>Replace or repair connector <xref wpid="M50026"/></item>
        
        peter guesses it's needed:
        <item>Replace or repair connector <xref 
        wpid="M50026"/></item>
        

        FIND = (?s)\x20(<xref.*?/>)
        REPLACE = \x20\($1\)
        MODE = regular expression

        gives:
        dd3e0de3-21cb-44d9-852b-c8a760966c1a-image.png

        not needed:
        <ref-setup-item><xref wpid="M50026"/></ref-setup-item>
        
        not needed:
        <para>Inspect cable identification band (<xref
        figid="T5A088-M8T01-036fig1"/>, Item 2).</para>
        
        needed:
        <item>Replace or repair connector (<xref wpid="M50026"/>)</item>
        
        peter guesses it's needed:
        <item>Replace or repair connector (<xref 
        wpid="M50026"/>)</item>
        

        I added a fourth condition with a newline in the needs-parentheses-xref, because based on your multiline doesnt-need, I thought you might also have multiline does-need. If not, change the (?s) to (?-s), and it will no longer match multiline xref.

        Also, in both the FIND and REPLACE I used \x20 – that’s regex-speak for character-at-hex-value 20, which is the space character at ASCII 32. I used that rather than a literal space in the search and replace to make it easier to see in the forum; in the regex, you can use either a literal space or the \x20 in either of those locations.

        The parentheses in the FIND are used for creating a group, so they do not mean literal parentheses in the search. The parentheses in the REPLACE would also have special meaning, except I “escaped” them by using \( and \)

        Richard HowardR 1 Reply Last reply Reply Quote 2
        • Richard HowardR
          Richard Howard @PeterJones
          last edited by

          @PeterJones
          Peter, that was a good guess! Yes, I’d say it’s possible the tagging could cross lines.
          I’ll give this a go and see how it works out.
          Appreciate it!

          Richard HowardR 1 Reply Last reply Reply Quote 1
          • Richard HowardR
            Richard Howard @Richard Howard
            last edited by

            @Richard-Howard
            Humm. It didn’t find any matches.
            Here is a code example it did not find:

            <para>Install LSB cover assembly <xref wpid="M40032"/>.</para>
            

            I’m using Find in Files
            9c221fd3-87b6-48b1-97c5-7d65ef025bfa-image.png

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @Richard Howard
              last edited by

              @Richard-Howard said in Adding parentheses around xml tags.:

              <para>Install LSB cover assembly <xref wpid=“M40032”/>.</para>

              It matches that text for me in a single file
              9f577cf3-1145-47ce-83c6-271d48755417-image.png

              I made two files, each containing:

              not needed:
              <ref-setup-item><xref wpid="M50026"/></ref-setup-item>
              
              not needed:
              <para>Inspect cable identification band (<xref
              figid="T5A088-M8T01-036fig1"/>, Item 2).</para>
              
              needed:
              <item>Replace or repair connector <xref wpid="M50026"/></item>
              
              peter guesses it's needed:
              <item>Replace or repair connector <xref
              wpid="M50026"/></item>
              
              <para>Install LSB cover assembly <xref wpid="M40032"/>.</para>
              

              Then I ran Find in Files > Find All, and it worked as expected, finding 3 instances in each that matched:
              a96d256d-6732-4862-ab29-85f7f3815446-image.png

              Richard HowardR 1 Reply Last reply Reply Quote 1
              • Richard HowardR
                Richard Howard @PeterJones
                last edited by

                @PeterJones
                So odd. I can’t see what I’m doing different/wrong?
                I tried a single file containing search criteria:

                para>Install LSB cover assembly <xref wpid="M40032"/>.</para>
                

                Still not finding it?
                66999a96-5582-47af-88b0-867a61a00472-image.png
                Ideas?

                Richard HowardR 1 Reply Last reply Reply Quote 0
                • Richard HowardR
                  Richard Howard @Richard Howard
                  last edited by

                  @Richard-Howard
                  I think I got it Peter.
                  I must have had a space in the front of the Find criteria.
                  It’s finding it now.
                  Thank you!!

                  Richard HowardR 1 Reply Last reply Reply Quote 1
                  • Richard HowardR
                    Richard Howard @Richard Howard
                    last edited by

                    @Richard-Howard
                    Just as a follow-up, this appears to have solved my need, saving me hours of edit time.
                    Thank you!!

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