A little help with a regex (find & replace) ?
-
Hi!
I don’t know how to use this regex thing… but if someone is kind and know how to help me, I am very thankful.
This is my document
microwave: value: initial restore: true kitchen_water_boiler: value: initial restore: true bed_motor: value: initial restore: true home_away: value: initial restore: true bedroom_heater: value: initial restore: true
And I want to find & replace lines which does not contain any space, as an example, I want from this:
bedroom_heater: value: initial restore: true
to this
variable_bedroom_heater: value: initial restore: true
Anyone can help me with this?
Thank you in advance!
-
Hello, @pickonedev and All,
OK… but which areas should be replaced ? I mean, do you want to change :
-
Any line without any
space
character, with a same string for all ( likevariable_bedroom_heater
, from your example ) ? -
Any line without any
space
character, with a string with some relation to the original name ( likevariable_microwave:
,variable_kitchen_water_boiler:
,variable_bed_motor:
, and so on… ) ? -
Any line without any
space
character, with a string totally different from the original name ( likedevice_1:
instead ofmicrowave:
,device_2:
instead ofkitchen_water_boiler:
and so on… ) ?
Best Regards,
guy038
-
-
Well… take a look at this
home_away: value: initial restore: true bedroom_heater: value: initial restore: true
I want them like this:
variable_home_away: value: initial restore: true variable_bedroom_heater: value: initial restore: true
I want to add “variable_” to every line which does not start with a space or tab.
Till now, I have tried with ^\S but it is not adding variable_, it is only replacing the first character, with variable_
I don’t know regex, I only try things till I have a result, but this is far over me :))
-
Hello, @pickonedev and All,
Ah… OK ! In that case, use the following regex S/R :
-
SEARCH
^[^ \r\n]+$
-
REPLACE
variable_$0
-
Tick the
Wrap around
option -
Select the
Regular espression
search mode -
Click on the
Replace All
button
This regex S/R mean :
-
Seacrh for any complete line, between its beginning (
^
) and its end ($
) with, at least, one character different from a space char and different from any EOL chars (\r
and\n
) -
And replace each of these lines with the string
variable_
, followed with the entire regex search, i.e.$0
BR
guy038
-
-
@guy038 Thank you so much.
Now, I have an idea :-D Just for the fun
If I have this:############## # Devices Status # ############## loading: value: loading restore: true bathroom_boiler: value: initial restore: true
and I want to change it to this
############## # Devices Status # ############## variable_loading: value: loading restore: true variable_bathroom_boiler: value: initial restore: true
To add “variable_” as I first requested, but if a line starts with # to exclude it
-
-
@pickonedev said in A little help with a regex (find & replace) ?:
I don’t know regex, I only try things till I have a result, but this is far over me
Useful References
-
If I don’t ask too much, can you please help me one more time?
I tried to find and learn by myself but it is far far far over me, this regex… I don’t get it…Here is a part from the document I have
service: switch.turn_off data: {} entity_id: switch.sonoff_computer_1 service: switch.turn_off data: {} entity_id: sonoff_computer_2
I want to add “sensor.variable_” to the lines which starts with "entity_id: " (even if there are spaces in front of it) but not contain the dot character " . "
For example, the document above, should look like this after the change:service: switch.turn_off data: {} entity_id: switch.sonoff_computer_1 service: switch.turn_off data: {} entity_id: **sensor.variable_**sonoff_computer_2
Thank you in advance
-
@pickonedev said in A little help with a regex (find & replace) ?:
I tried to find and learn by myself but it is far far far over me
When you set out to learn the piano, you should start with “Row, Row, Row Your Boat” and “London Bridge”, and work your way up through Canon in D to Chopin and Liszt, over years of practice.
The lesson: If you try to start with the more complicated, you will only end up discouraging yourself. Sometimes, with regex, it’s better to get a portion of it working, and then do the rest manually (so maybe try to search for something more generic that you can find, then use your own eyes to do the rest. Then, after more experimentation, you can figure out how to do more and more, until you can come up with pretty powerful regex.
I want to add “sensor.variable_” to the lines which starts with "entity_id: " (even if there are spaces in front of it) but not contain the dot character " . "
Okay, this isn’t a full concerto, but it is an intermediate etude.
Break it into smaller chunks for developing:
- line starts with
entity_id:
even if there are spaces =>^(\h*entity_id:)
^
= line starts with\h*
= 0 or more spacesentity_id:
= literal text(...)
= we want to save this chunk for later; it will be group#1 and referenced later as$1
- but the rest of the line does not contain the dot character =>
([^.\r\n]+)$
(...)
= save the rest of the line as group#2 (later called$2
)[^...]
= make a “negative” character class, to define a list of characters that you don’t want to match[^.\r\n]
= don’t want to match a literal period, or the CR or LF newline characters+
= match 1 or more of the thing that came before – so in this case, “match 1 or more characters that are not period or newline characters”$
= match the end of the line- thus, the only thing allowed for this portion of the search are characters that are not period or newline, and that extends all the way to the end of the line. It will thus match if the line doesn’t have a dot, and won’t match if it does.
- Put it together: you want the first group, followed by a space, followed by the second group:
^(\h*entity_id:) ([^.\r\n]+)$
- Replacement: you want to replace it with the first group, then a space, then the new text, then the second group:
$1 sensor.variable_$2
----
Useful References
- Notepad++ Online User Manual: Searching/Regex
- FAQ: Where to find other regular expressions (regex) documentation
----
Please note: This Community Forum is not a data transformation service; you should not expect to be able to always say “I have data like X and want it to look like Y” and have us do all the work for you. If you are new to the Forum, and new to regular expressions, we will often give help on the first one or two data-transformation questions, especially if they are well-asked and you show a willingness to learn; and we will point you to the documentation where you can learn how to do the data transformations for yourself in the future. But if you repeatedly ask us to do your work for you, you will find that the patience of usually-helpful Community members wears thin. The best way to learn regular expressions is by experimenting with them yourself, and getting a feel for how they work; having us spoon-feed you the answers without you putting in the effort doesn’t help you in the long term and is uninteresting and annoying for us.
- line starts with