Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I need a regex to strip trailing whitespace from log lines. I’m testing a small text-cleanup script and want to preserve everything else on each line.
[assistant]
Use this regex to match trailing spaces and tabs:
```regex
[ \t]+$
```
Replace each match with an empty string. In multiline mode, `$` applies at the end of each line. For example:
```text
input: "INFO started "
output: "INFO started"
input: "WARN retry\t\t"
output: "WARN retry"
input: "DEBUG value = 42"
output: "DEBUG value = 42"
```
If you also want to remove other horizontal whitespace characters, use `\h+$` if your regex engine supports `\h`. The `[ \t]+$` version is more portable.
[user]
Does that handle Windows line endings? I have a test file with CRLF, and I don’t want the carriage return to interfere with the match.
[assistant]
It depends on the regex engine and its multiline behavior. A portable pattern that explicitly avoids consuming the line ending is:
```regex
[ \t]+(?=\r?$)
```
Replace matches with an empty string. The lookahead checks that the whitespace is followed by either an optional carriage return and the end of the line.
For a CRLF input represented as:
```text
INFO started \r\n
WARN retry\t\r\n
OK\r\n
```
the result is:
```text
INFO started\r\n
WARN retry\r\n
OK\r\n
```
If your engine treats `$` as matching before `\n` but not before `\r`, the lookahead is useful because it allows the `\r` to remain part of the line ending.
[user]
Can you show this in Python? I’d like a function that accepts a whole string and keeps the original line-ending style.
[assistant]
Yes. Use `re.sub` with a multiline pattern and avoid matching the carriage return:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(r"[ \t]+(?=\r?$)", re.MULTILINE)
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
Example:
```python
sample = (
"INFO started \r\n"
"WARN retry\t\t\r\n"
"DEBUG value = 42 \n"
"DONE\t"
)
cleaned = strip_trailing_whitespace(sample)
print(repr(cleaned))
```
Output:
```text
'INFO started\r\nWARN retry\r\nDEBUG value = 42\nDONE'
```
The function does not split and rejoin the text, so it preserves CRLF, LF, and any final-line behavior. It only removes spaces and tabs immediately before a line ending or the end of the string.
[user]
What about blank lines containing spaces? I want those spaces removed too, but I also want a line that contains only a tab to become an empty line rather than disappear.
[assistant]
The same pattern handles that case. A line containing only spaces or tabs has a match from its beginning through the whitespace, and the replacement is empty. The newline is not matched.
```python
cases = [
" \n",
"\t\n",
" \t \r\n",
"\n",
"\r\n",
]
for value in cases:
result = strip_trailing_whitespace(value)
print(repr(value), "->", repr(result))
```
Output:
```text
' \n' -> '\n'
'\t\n' -> '\n'
' \t \r\n' -> '\r\n'
'\n' -> '\n'
'\r\n' -> '\r\n'
```
So the line remains present. Only its horizontal whitespace is removed.
[user]
I’m seeing a problem with a final line that has no newline. In one test, `"DONE\t"` stays unchanged. Could this be related to the `$` behavior?
[assistant]
Yes. In Python, with `re.MULTILINE`, `$` can match before a final newline as well as at the absolute end, but the interaction with the lookahead can be confusing around line endings and empty matches. A clearer Python-specific pattern is to use a lookahead for either a line ending or the true end of the string:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(
r"[ \t]+(?=\r?$|\Z)",
re.MULTILINE,
)
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
However, because `$` already covers the end of a line in multiline mode, the simpler version normally handles `"DONE\t"`:
```python
re.sub(r"[ \t]+(?=\r?$)", "", "DONE\t", flags=re.MULTILINE)
```
You can inspect the result directly:
```python
value = "DONE\t"
print(repr(re.sub(r"[ \t]+(?=\r?$)", "", value, flags=re.MULTILINE)))
```
Expected output:
```text
'DONE'
```
If your actual input has a final newline, `repr()` will make that visible, for example `'DONE\r\n'`.
[user]
Can we make the pattern less dependent on multiline mode? I’m comparing a couple of implementations and want the line boundary to be explicit.
[assistant]
Yes. You can match horizontal whitespace followed by either a CRLF, an LF, or the end of the string, without using multiline mode:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(r"[ \t]+(?=\r\n|\n|\Z)")
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
This gives the boundary alternatives explicitly:
- `\r\n` — a Windows-style line ending
- `\n` — a Unix-style line ending, including the LF portion of a CRLF if considered alone
- `\Z` — the actual end of the string
Test:
```python
sample = (
"INFO started \r\n"
"WARN retry\t\t\r\n"
"DEBUG value = 42 \n"
"blank line\t\n"
"DONE\t"
)
print(repr(strip_trailing_whitespace(sample)))
```
Output:
```text
'INFO started\r\nWARN retry\r\nDEBUG value = 42\nblank line\nDONE'
```
The lookahead leaves every line-ending character untouched.
[user]
Would `\s+` be shorter, or would that accidentally remove line breaks?
[assistant]
`\s+` is usually too broad for this task. In Python, `\s` matches spaces, tabs, newlines, carriage returns, and several other whitespace characters. Depending on the surrounding pattern, it can consume line endings and even whitespace spanning multiple lines.
For example:
```python
import re
value = "first \nsecond\t\nthird"
print(repr(re.sub(r"\s+$", "", value)))
```
The result depends on the flags and pattern context, but it is not limited to horizontal whitespace. A safer short form, if you only need ordinary spaces and tabs, is:
```python
re.sub(r"[ \t]+(?=\r\n|\n|\Z)", "", value)
```
If you want Unicode horizontal whitespace, Python’s standard `re` module does not provide a universally portable `\h` class. You can use an explicit character class, but the exact set should match your input requirements. For ordinary logs, `[ \t]` avoids changing line structure.
[user]
I also need to account for form-feed characters because one generated log includes them as indentation. Should they be treated like trailing whitespace?
[assistant]
If form-feed should be removed when it appears at the end of a log line, add `\f` to the character class:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(r"[ \t\f]+(?=\r\n|\n|\Z)")
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
Example:
```python
sample = (
"INFO started \f\r\n"
"WARN retry\t\f\n"
"DEBUG value = 42\f"
)
print(repr(strip_trailing_whitespace(sample)))
```
Output:
```text
'INFO started\r\nWARN retry\nDEBUG value = 42'
```
This treats spaces, tabs, and form-feed as removable horizontal or layout whitespace, while leaving `\r\n` and `\n` intact.
If form-feed can occur in the middle of meaningful content, this pattern leaves it alone because it only matches a run immediately before a recognized line boundary.
[user]
Could you add tests that cover CRLF, LF, no final newline, blank whitespace-only lines, and internal spaces?
[assistant]
Here is a small `unittest` suite:
```python
import re
import unittest
_TRAILING_HORIZONTAL_WS = re.compile(r"[ \t\f]+(?=\r\n|\n|\Z)")
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
class StripTrailingWhitespaceTests(unittest.TestCase):
def test_crlf(self):
value = "INFO started \r\nWARN retry\t\r\nDONE\r\n"
expected = "INFO started\r\nWARN retry\r\nDONE\r\n"
self.assertEqual(strip_trailing_whitespace(value), expected)
def test_lf(self):
value = "INFO started \nWARN retry\t\nDONE\n"
expected = "INFO started\nWARN retry\nDONE\n"
self.assertEqual(strip_trailing_whitespace(value), expected)
def test_no_final_newline(self):
value = "INFO started \nDONE\t"
expected = "INFO started\nDONE"
self.assertEqual(strip_trailing_whitespace(value), expected)
def test_whitespace_only_lines_remain(self):
value = "first\n \n\t\f\r\nlast"
expected = "first\n\n\r\nlast"
self.assertEqual(strip_trailing_whitespace(value), expected)
def test_internal_spaces_are_preserved(self):
value = "key = value with spaces \n"
expected = "key = value with spaces\n"
self.assertEqual(strip_trailing_whitespace(value), expected)
if __name__ == "__main__":
unittest.main()
```
Run it with:
```text
$ python -m unittest test_strip_ws.py -v
test_crlf (test_strip_ws.StripTrailingWhitespaceTests) ... ok
test_internal_spaces_are_preserved (test_strip_ws.StripTrailingWhitespaceTests) ... ok
test_lf (test_strip_ws.StripTrailingWhitespaceTests) ... ok
test_no_final_newline (test_strip_ws.StripTrailingWhitespaceTests) ... ok
test_whitespace_only_lines_remain (test_strip_ws.StripTrailingWhitespaceTests) ... ok
----------------------------------------------------------------------
Ran 5 tests in 0.001s
OK
```
The internal double spaces remain because the pattern only matches whitespace that is immediately followed by `\r\n`, `\n`, or the end of the string.
[user]
One concern: my input can contain old-style Mac line endings, just `\r`. Does the explicit pattern need another alternative?
[assistant]
Yes. If bare carriage returns are valid line endings, include `\r` as a boundary alternative:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(
r"[ \t\f]+(?=\r\n|\n|\r|\Z)"
)
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
Put `\r\n` before the individual `\r` alternative. The regex engine will try the two-character CRLF boundary first, so it does not treat the `\r` in CRLF as a separate bare line ending.
Example:
```python
sample = (
"INFO started \r"
"WARN retry\t\r"
"DEBUG value = 42\f"
)
print(repr(strip_trailing_whitespace(sample)))
```
Output:
```text
'INFO started\rWARN retry\rDEBUG value = 42'
```
The line separators are retained, including the bare carriage returns.
[user]
Is there a standard-library alternative that avoids regex, especially if the text is large?
[assistant]
Yes. A line-oriented approach can be readable, but use `splitlines(keepends=True)` carefully because it recognizes several line-ending forms. This version removes only spaces, tabs, and form-feed immediately before each retained line ending:
```python
def strip_trailing_whitespace_lines(text: str) -> str:
parts = text.splitlines(keepends=True)
result = []
for part in parts:
if part.endswith("\r\n"):
body, ending = part[:-2], "\r\n"
elif part.endswith(("\n", "\r")):
body, ending = part[:-1], part[-1]
else:
body, ending = part, ""
result.append(body.rstrip(" \t\f") + ending)
return "".join(result)
```
Example:
```python
sample = "A \r\nB\t\n \rC\f"
print(repr(strip_trailing_whitespace_lines(sample)))
```
Output:
```text
'A\r\nB\n\rC'
```
For very large files, process the file incrementally rather than loading the whole string:
```python
with open("input.log", "r", newline="") as source, \
open("output.log", "w", newline="") as target:
for line in source:
if line.endswith("\r\n"):
target.write(line[:-2].rstrip(" \t\f") + "\r\n")
elif line.endswith(("\n", "\r")):
target.write(line[:-1].rstrip(" \t\f") + line[-1])
else:
target.write(line.rstrip(" \t\f"))
```
Using `newline=""` prevents universal-newline translation, so the original line-ending characters are written back.
[user]
I want to keep the regex version. Can you summarize the final pattern and the main edge cases in a compact form?
[assistant]
Final Python version:
```python
import re
_TRAILING_HORIZONTAL_WS = re.compile(
r"[ \t\f]+(?=\r\n|\n|\r|\Z)"
)
def strip_trailing_whitespace(text: str) -> str:
return _TRAILING_HORIZONTAL_WS.sub("", text)
```
Pattern parts:
```text
[ \t\f]+ one or more spaces, tabs, or form-feed characters
(?=...) lookahead; do not consume the boundary
\r\n CRLF line ending
\n LF line ending
\r bare CR line ending
\Z actual end of the string
```
Behavior:
```python
tests = [
"A \r\n", # -> "A\r\n"
"B\t\n", # -> "B\n"
"C\f\r", # -> "C\r"
"D\t", # -> "D"
" \n", # -> "\n"
"key = value \n", # -> "key = value\n"
]
for value in tests:
print(repr(value), "->", repr(strip_trailing_whitespace(value)))
```
It preserves CRLF, LF, and bare CR separators; keeps whitespace inside a line; preserves blank lines; and removes trailing spaces, tabs, and form-feed characters. Avoid `\s+` here because it includes line-ending characters and is broader than the requested cleanup.