• Login
Community
  • Login

Regex find single N occurrence of ',' to replace

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
regex occurance
5 Posts 3 Posters 1.2k 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.
  • N
    Nom
    last edited by Aug 24, 2019, 5:31 AM

    I’m wanting to do something seemingly simple, but I’m having a hard time creating the expression.

    Say I have this text:
    ANY,data,OF,any,SIZE,may,BE,inbetween

    I’d like to select say the 4th comma and only that character:
    ANY,data,OF,any**,**SIZE,may,BE,inbetween

    I’m probably overlooking something fundamental in regex.
    Thank you.

    A 1 Reply Last reply Aug 24, 2019, 12:40 PM Reply Quote 0
    • A
      Alan Kilborn @Nom
      last edited by Aug 24, 2019, 12:40 PM

      @Nom said:

      I’d like to select say the 4th comma and only that character

      This seems overcomplicated, but it also seems to do the job:

      Find what: ^(?-s)(?:.*?,){3}.*?(?=,)\K.

      1 Reply Last reply Reply Quote 3
      • N
        Nom
        last edited by Aug 25, 2019, 2:09 PM

        Thank you, regex seems to have its limits in regards to simplicity in certain use-cases.

        1 Reply Last reply Reply Quote 2
        • P
          PeterJones
          last edited by Aug 25, 2019, 6:17 PM

          @Nom said:

          regex seems to have its limits in regards to simplicity in certain use-cases.

          That’s like saying “C has certain limits in regards to simplicity in certain use-cases” or “the English language has certain limits in regards to simplicity in certain use-cases”. The more powerful something is, it sometimes makes it harder to express it “simply”.

          The regex was able to express in 28 characters something you used 341 characters to describe. It used only 5 or 6 different elements of syntax to express that. It’s incredibly simpler than what you would have to write in raw C (ie, no external libraries) or assembler to do the same thing. Or even the English-based algorithm you’d have to write down to accomplish the same task, accounting for all the same edge cases that the regex syntax is naturally handling.

          A 1 Reply Last reply Aug 25, 2019, 6:25 PM Reply Quote 0
          • A
            Alan Kilborn @PeterJones
            last edited by Aug 25, 2019, 6:25 PM

            Okay, okay, everyone has called attention to my regex…now @guy038 will come along and do the same solution in about half as many characters. :(

            BTW, I made a fat-finger mistake in the regex, but the beauty of it is that it still works. [The final character should be , (comma) instead of . (period).]

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