@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 spaces
entity_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.