Script Search And Replace for multi lines

Hi all, i’m new in python and would like to modify this code for multiple lines…
Could someone please help me?

the code :

import re

from ..Script import Script

class SearchAndReplace(Script):
    def getSettingDataString(self):
        return """{
            "name": "Search and Replace",
            "key": "SearchAndReplace",
            "metadata": {},
            "version": 2,
                    "label": "Search",
                    "description": "All occurrences of this text will get replaced by the replacement text.",
                    "type": "str",
                    "default_value": ""
                    "label": "Replace",
                    "description": "The search text will get replaced by this text.",
                    "type": "str",
                    "default_value": ""
                    "label": "Use Regular Expressions",
                    "description": "When enabled, the search text will be interpreted as a regular expression.",
                    "type": "bool",
                    "default_value": false

    def execute(self, data):
        search_string = self.getSettingValueByKey("search")
        if not self.getSettingValueByKey("is_regex"):
            search_string = re.escape(search_string) 
        search_regex = re.compile(search_string)

        replace_string = self.getSettingValueByKey("replace")

        for layer_number, layer in enumerate(data):
            data[layer_number] = re.sub(search_regex, replace_string, layer) 

        return data

Thx in advance

There are already, a large number of editors that have this feature and as such, you seem to be trying to “reinvent the wheel”, so to speak. I’m not saying that could not learn something from doing that, but I do wonder if your efforts would be better placed elsewhere.

It’s for a script for editing G-Code in Cura for 3d printing

That’s fair enough, but G-Code is simply another structured coding system, written in ASCII text, and as such I would have thought that the code can be edited with any code editor. Is that not the case?

yes, that’s the case, but I’d like to integrate the script into the cura software.

1 Like
  • Simple (literal) string replacements are generally at least 5x as fast as regexes, so it might be worthwhile to make separate clauses for the regex and non-regex replacements, expecially if your texts are long and/or you have many search/replace pairs.
  • If you can use the 3d-party regex package (a plug-in replacement for re), the regexes will be executed faster and you get a lot more supported features in regexes. (The only change your code would need is import regex as re.)
  • The code as-is already supports multiline search and replace. That is – unless you want to support the special characters ‘^’ and ‘$’ to indicate start and end of lines (when using the re.MULTILINE flag). Note that users can also specify the regex search flags in the regex pattern string itself. For instance re.MULTILINE can also be specified as (?m)some_pattern. (See the flags (?aiLmsux) in the re docs).
1 Like