I have a string in a django/python app and need to search it for every instance of [x]. This includes the brackets and x, where x is any number between 1 and several million. I then need to replace x with my own string.
ie, 'Some string [3423] of words and numbers like 9898' would turn into 'Some string [mycustomtext] of words and numbers like 9898'
Note only the bracketed number was affected. I am not familiar with regex but think this will do it for me?
Regex is precisely what you want. It’s the re module in Python, you’ll want to use re.sub, and it will look something like:
If you need to do a lot of it, consider compiling the regex:
Edit: To reuse the number, use a regex group:
Compilation is still possible too.