I try to refactor python code written by someone else. QA does not want tabs, hence I replaced them by four whitespaces, but there were also a lot of tabs used inside the code, and not only for indentation at beginnings of lines.
Here is a sample piece of code:
def post(self, request, format=None):
action = request.query_params.get('action', None)
res = 'UNKNOWN ACTION'
hhtpstatus = status.HTTP_400_BAD_REQUEST
I tried these regex:
[^\S]([ ]{2,})[^\S]
This one selects correctly the multiple spaces inside the code, but select also the ones used for indentation, which I don't want. If I use this one, I'll loose the indentations...
[^\s]([ ]{2,})[^\S]
This one is better, as it only selects the multiple spaces inside the code, but takes the last character of the preceding code, which obviously I don't want neither. If I use this, I ruin all the code by scraping the last character with the multiple space following it...
I work in Atom, and the regex should detect only the multiple whitespaces encompassed by code before and code after, and select only the spaces, so I can replace them by single space
I also tried with [^\W]
and [^\w]
with less success: small capital still detects indentation spaces, and select all the non words code characters (, : = and so on). Big capital selects only spaces following words, not spaces following other code characters, and selects also the last word character before multiple space...
Is there so a way to use [^\s]([ ]{2,})[^\S]
, which detects properly only the multiple spaces inside the code, but modify it to contain/select ONLY spaces, and not last preceding code character? So begin selection one character later, if this makes sense...
CodePudding user response:
You can change [^\s]
to \S
and change [^\S]
to \s
and assert them instead using lookarounds.
If you match 1 or more spaces, and assert a single space to the right, you will keep 1 space when replacing with an empty string.
(?<=\S)[ ] (?=\s)
If you don't want to match the spaces at the end when there is no whitespace character following, you can match 2 or more spaces and assert a non whitspace char to the right.
Then in the replacement you can use a single space.
(?<=\S)[ ]{2,}(?=\S)