Find and Replace with variable within RegEx
-
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')
withoutnot in ['unavailable', 'unknown']
, which shouldn’t be matched, so simply doing this in two steps by first only replacingstates
withhas_value
and thennot 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
andnot 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')
? -
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!
-
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
-
-
@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 -
@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.) -
@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:\\] \\[
-
@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.
Here are some images from the FAQ showing the
edit & preview:
rendering
-
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
-
@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.
-
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