Community
    • Login

    Regex help with replacement

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    regex
    13 Posts 3 Posters 1.1k Views 2 Watching
    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 Offline
      Acme1235 @PeterJones
      last edited by

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • A Offline
        Acme1235 @guy038
        last edited by

        @guy038 the ID’s are usually different. That’s why I need to search for the number at the bottom. Thanks for the help though!! @PeterJones hit it spot on

        1 Reply Last reply Reply Quote 0
        • A Offline
          Acme1235 @PeterJones
          last edited by

          @PeterJones I am having one problem. I have multiple sections of this code and it wants to grab all sections under the first g instead of just that section.

          A 1 Reply Last reply Reply Quote 0
          • A Offline
            Acme1235 @Acme1235
            last edited by

            @Acme1235 said in Regex help with replacement:

            @PeterJones I am having one problem. I have multiple sections of this code and it wants to grab all sections under the first g instead of just that section.

            Fixed it by adding a ? In front of the \s. Thanks again!

            PeterJonesP 1 Reply Last reply Reply Quote 1
            • PeterJonesP Offline
              PeterJones @Acme1235
              last edited by

              @Acme1235 ,

              Great job in taking the lessons learned and figuring out how to tweak it. We like it when people take that initiative to try to update the regex themselves! Plus, it’s good for you, because it means you are learning.

              Good luck.

              1 Reply Last reply Reply Quote 0
              • guy038G Online
                guy038
                last edited by guy038

                Hello, @acme1235,

                My bad :-(( I didn’t read carefully. Indeed , you said :

                I’m trying to capture a number at the bottom of the code shown (>4< )

                So, here is my new version, which is a bit different from Peter’s one, because I use a look-ahead which captures the correct number before </tspan>, in group 2. So the .+? syntax, right before the look-ahead, is just the part Id="••" ( where • stands for a digit ), which is to be changed !

                SEARCH (?s-i)(<g.*?\s*).+?(?=>.+?>(\d+)</tspan>)

                REPLACE $1xid="$2"\x20xname="$2"


                Note that this S/R would change the line under the <g line, whatever its value, with the correct replacement :

                For instance, from the initial text :

                <g
                This is a test>
                <text
                Xml:space="preserve"
                X="422"
                Y="311"
                ><tspan 
                X="1100"
                Y="300">7</tspan></text>
                </g>
                

                we would obtain :

                <g
                xid="7" xname="7">
                <text
                Xml:space="preserve"
                X="422"
                Y="311"
                ><tspan 
                X="1100"
                Y="300">7</tspan></text>
                </g>
                

                And this also means that if you run this S/R twice, You still get the right replacement ;-))

                Cheers,

                guy038

                P.S. : I’ve just verified that the Peter’s S/R has exactly the same behaviour !

                A 1 Reply Last reply Reply Quote 2
                • A Offline
                  Acme1235 @guy038
                  last edited by

                  @guy038 awesome!! I was wondering if I could use a look ahead to do the same thing. Thanks again for the help!

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    Acme1235 @PeterJones
                    last edited by

                    @PeterJones sorry to bug you and reopen this again. The regex works awesome, I’ve even tweaked it a little bit. The problem I’m having is it’s catching the beginning g tag with the next g tag. I tried to write an look around exception shown below, but I don’t know where to stick it in the regex written or if there is a better way to exclude the <rect portion.

                    The look around I wrote is:

                    ^((?!rect).)*$

                    This is the problem I’m having in the code.

                    <g
                    <rect 
                    Width="256"
                    Height="256"
                    <g
                    Id="4">
                    <text
                    Xml:space="preserve"
                    X="422"
                    Y="311"
                    ><tspan 
                    X="1100"
                    Y="300">4</tspan></text>
                    </g>
                    
                    </g>
                    
                    

                    It grabs everything from the first <g tag.

                    PeterJonesP 1 Reply Last reply Reply Quote 0
                    • PeterJonesP Offline
                      PeterJones @Acme1235
                      last edited by

                      @Acme1235 said in Regex help with replacement:

                      I don’t know where to stick it in the regex … The look around I wrote is:
                      ^((?!rect).)*$

                      So yes, that sub-expression is trying to find sequences of characters that don’t include rect.

                      Looking at my regex (?s-i)<g[^>]*(>.*\s*">)(\d+)(<), the place I would put it is instead of (or in conjunction with) the [^>]*. It originally said “look for 0 or more non-> characters”. You want to modify that to say “look for 0 or more non-> characters, as long those characters do not include rect”

                      So, let’s merge: the [^>] will take the place of . in your sub-expression (because we don’t want to match >); and then then the sub-expression will take the place of[^>]* in my expression. That combines to (?s-i)<g(?:(?!rect)[^>])*(>.*\s*">)(\d+)(<), which finds
                      5a1e56ba-7ec7-4528-8f38-a73ddad59b77-image.png

                      (I used the ?: in the outer parentheses to make sure it didn’t change the group# for the matches in your replacement expression – (?:...) is the syntax for non-capturing group)

                      Unfortunately, it’s hard to parse XML in regex (and, in general, a bad idea). But it’s really hard to parse bad/broken XML, like your examples with incomplete tags.

                      A 1 Reply Last reply Reply Quote 0
                      • A Offline
                        Acme1235 @PeterJones
                        last edited by

                        @PeterJones awesome thank you so much! Learning regex is a long marathon lol

                        1 Reply Last reply Reply Quote 1

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors