Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Find and Replace with variable within RegEx

    Help wanted · · · – – – · · ·
    4
    10
    64
    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.
    • nagyrobi
      nagyrobi last edited by nagyrobi

      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
      • nagyrobi
        nagyrobi last edited by

        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!

        Mark Olson 1 Reply Last reply Reply Quote 4
        • guy038
          guy038 last edited by guy038

          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
          • Mark Olson
            Mark Olson @nagyrobi last edited by PeterJones

            @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

            PeterJones 1 Reply Last reply Reply Quote 0
            • PeterJones
              PeterJones @Mark Olson last edited by PeterJones

              @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.)

              Mark Olson 1 Reply Last reply Reply Quote 4
              • Mark Olson
                Mark Olson @PeterJones last edited by

                @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: \] \[

                PeterJones 1 Reply Last reply Reply Quote 2
                • PeterJones
                  PeterJones @Mark Olson last edited by PeterJones

                  @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
                  • guy038
                    guy038 last edited by guy038

                    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

                    PeterJones 1 Reply Last reply Reply Quote 0
                    • PeterJones
                      PeterJones @guy038 last edited by PeterJones

                      @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
                      • guy038
                        guy038 last edited by

                        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
                        • First post
                          Last post
                        Copyright © 2014 NodeBB Forums | Contributors