• Login
Community
  • Login

Find and Replace with variable within RegEx

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
10 Posts 4 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
    nagyrobi
    last edited by nagyrobi May 4, 2023, 12:13 PM May 4, 2023, 12:02 PM

    Dear community,

    I need to find all occurrences of

    states('cover.yourroom') not in ['unavailable', 'unknown']
    states('light.myroom') not in ['unavailable', 'unknown']
    ...
    states('any.entity') not in ['unavailable', 'unknown']
    

    to

    has_value('cover.yourroom')
    has_value('light.myroom')
    ...
    has_value('any.entity')
    

    There are hundreds of occurrences where only the string between the parantheses and single quotes is different (eg. ('any.entity')).

    So the thing in common is that the string between (' and ') must be kept, and the rest must be replaced.

    Unfortunately there are other matches with states('any.entity') without not in ['unavailable', 'unknown'], which shouldn’t be matched, so simply doing this in two steps by first only replacing states with has_value and then not in ['unavailable', 'unknown'] with nothing would not give the correct result, as the first step would replace undesired matches.

    We have to exclusively match the strings containing both states and not in ['unavailable', 'unknown'] in the same time, have a variable string between them, and keep that variable string after replacement.

    It seems that a RegEx suitable to Find these strings could be states\([^)]*\) not in ['unavailable', 'unknown'], but what do I use in the Replace field to keep the part of the string found between (' and ')?

    1 Reply Last reply Reply Quote 0
    • N
      nagyrobi
      last edited by May 4, 2023, 1:01 PM

      OK, this seems to work:
      Find: (states\(')([^)]*)('\) not in \['unavailable', 'unknown'\])
      Replace:has_value\('$2'\)

      Using Placeholder Sequences of boost regex library.

      So we need to put between () the sub-expressions of the regex, in my case the second subexpression gives the entity string I wish to keep, and with $2 I can reference to it in the Replace field.

      Awesome! Thanks!

      M 1 Reply Last reply May 4, 2023, 6:40 PM Reply Quote 4
      • G
        guy038
        last edited by guy038 May 4, 2023, 7:00 PM May 4, 2023, 6:23 PM

        Hello, @nagyrobi and All,

        or… simply :

        SEARCH (?-s)^states(\(.+\)).+    or    (?x-s) ^ states ( \( .+ \) ) .+

        REPLACE has_value$1


        NOTES : the initial in-line modifiers mean :

        • (?-s) : The . dot regex char matches standard characters ONLY ( NOT EOL chars )

        • (?x) : The free-spacing is invoked :

          • Any space character does not count, except if written as [ ] or \x20 or escaped with a \ char

          • Any tab character does not count, except if written as [ ] or \t or escaped with a \ char

          • The first # char of each line, possibly preceded with blank chars, starts a comment sequence, except if written [#] or \x23 or \#

        For instance, the search regex, below, is fully functional :

        (?x-s) ^ states # a comment
                        # a second comment
        		# a third  comment
        ( \( .+ \) )
        # last comment
        .+
        

        Note that the replacement regex does not support the free-spacing mode !

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 1
        • M
          Mark Olson @nagyrobi
          last edited by PeterJones May 4, 2023, 6:52 PM May 4, 2023, 6:40 PM

          @nagyrobi
          Glad you figured it out! I’d just observe that you don’t need to use () to enclose every part of the regex, and since you’re not using the first or second capture groups, you can slightly simplify it like so:

          (?-i)states(\('[^']*'\)) not in \\['unavailable', 'unknown'\\]
          

          The only other change I made was to escape the square brackets in \\['unavailable', 'unknown'\\] so that they’re correctly interpreted as the literal characters [ and ].

          –
          moderator edit: fixed escaping

          P 1 Reply Last reply May 4, 2023, 6:48 PM Reply Quote 0
          • P
            PeterJones @Mark Olson
            last edited by PeterJones May 4, 2023, 6:54 PM May 4, 2023, 6:48 PM

            @Mark-Olson said in Find and Replace with variable within RegEx:

            he only other change I made was to escape the square brackets

            @Mark-Olson , just so you know, the OP may have already had them escaped originally. The forum messes up backslash-[ and backslash-] even inside red text or code/plaintext blocks, and getting the escaping so it “looks” right in the NodeBB Forum software is a challenge. This is described in the SPECIAL CHARACTERS section of our formatting-posts FAQ. For example, I had to use my moderator powers to add in the right number of backslashes in your post, so that your claim that you added in escaping didn’t look ridiculously wrong – because your backslashes were not visible after the post was made. (And if you edit your post, it will unfix itself.)

            M 1 Reply Last reply May 4, 2023, 7:13 PM Reply Quote 4
            • M
              Mark Olson @PeterJones
              last edited by May 4, 2023, 7:13 PM

              @PeterJones
              Yeah, I was aware of this issue and I paid special attention to it when I posted and still got it wrong. Sorry!

              The worst part is that the side-by-side preview of your post doesn’t have this problem, and it only materializes after I hit submit.

              OK, I’m experimenting now:
              one dash before: \] \[
              two dashes before: \\] \\[

              P 1 Reply Last reply May 4, 2023, 7:21 PM Reply Quote 2
              • P
                PeterJones @Mark Olson
                last edited by PeterJones May 4, 2023, 7:23 PM May 4, 2023, 7:21 PM

                @Mark-Olson said in Find and Replace with variable within RegEx:

                The worst part is that the side-by-side preview of your post doesn’t have this problem, and it only materializes after I hit submit.

                As it says in the FAQ.

                2c4fa462-7e5e-4a69-abb2-78075f76ec60-image.png

                Here are some images from the FAQ showing the

                edit & preview:

                rendering

                1 Reply Last reply Reply Quote 2
                • G
                  guy038
                  last edited by guy038 May 4, 2023, 7:37 PM May 4, 2023, 7:35 PM

                  Hello, @mark-olson, @peterjones and All,

                  Note that if you edit your post to add / change / delete anything else, you must redo, each time, the modifications regarding all the [ and/or ] characters of your post, if any !!

                  BR

                  guy038

                  P 1 Reply Last reply May 4, 2023, 7:37 PM Reply Quote 0
                  • P
                    PeterJones @guy038
                    last edited by PeterJones May 4, 2023, 7:38 PM May 4, 2023, 7:37 PM

                    @guy038 ,

                    As I already said, both in my post an hour ago and in the FAQ.

                    Sorry to the OP (“Original Poster”) for allowing your thread to get hijacked.

                    1 Reply Last reply Reply Quote 3
                    • G
                      guy038
                      last edited by May 4, 2023, 7:54 PM

                      Hi, @peterjones,

                      Sorry, Peter, but as I well used to posting on our forum, I has not had a look, yet, to the FAQ about formatting :-((

                      And, indeed, all is very well explained in this FAQ ;-)) So, my previous post is redundant and useless !

                      BR

                      guy038

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