Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I need a regex to strip trailing whitespace from log lines. I’m testing against a plain-text file and want spaces and tabs removed only at the end of each line, without changing indentation or internal spacing.
[assistant]
Use this pattern in multiline mode:
```regex
[ \t]+$
```
Replace every match with an empty string. The character class limits the match to ordinary spaces and tabs, and `$` anchors it to the end of each line when multiline mode is enabled. For example, in a PCRE-style tool:
```text
Pattern: [ \t]+$
Options: m
Replacement: (empty)
```
This does not match whitespace in the middle of a line.
[user]
What would the test strings and expected output look like? I also have blank lines, and I want to know whether the regex removes anything from them.
[assistant]
Here is a compact test set. The visible markers make trailing characters easier to inspect:
```text
Input:
<INFO> started >
<DEBUG> value=42\t\t>
<NOTICE> keep internal spaces >
<blank line>
<spaces-only line> >
<tab-only line>\t\t>
<INFO> finished>
Expected after replacing [ \t]+$ with nothing:
<INFO> started>
<DEBUG> value=42>
<NOTICE> keep internal spaces>
<blank line>
<spaces-only line>>
<tab-only line>>
<INFO> finished>
```
In that display, `>` is just a marker and is not part of the input. A genuinely empty line remains empty. A line containing only spaces or tabs becomes empty because the entire whitespace run matches. If you want to preserve whitespace-only lines, use a pattern that requires a non-whitespace character before the trailing run:
```regex
(?<=\S)[ \t]+$
```
That version removes trailing spaces or tabs only from lines that contain a non-whitespace character.
[user]
Can you show the same test with escaped representations so tabs and line endings are unambiguous?
[assistant]
Sure. Treat each listed item as one line, with `\t` representing a tab and `\n` representing the line ending in the serialized form:
```text
Input lines:
1. "<INFO> started \n"
2. "<DEBUG> value=42\t\t\n"
3. "<NOTICE> keep internal spaces \n"
4. "\n"
5. " \n"
6. "\t\t\n"
7. "<INFO> finished\n"
Output with [ \t]+$ in multiline mode:
1. "<INFO> started\n"
2. "<DEBUG> value=42\n"
3. "<NOTICE> keep internal spaces\n"
4. "\n"
5. "\n"
6. "\n"
7. "<INFO> finished\n"
```
The line-ending `\n` is not removed. The regex matches only the spaces or tabs before it. For a file using CRLF line endings, many engines still handle `$` correctly in multiline mode, but behavior varies. If needed, use:
```regex
[ \t]+(?=\r?$)
```
This explicitly allows an optional carriage return before the end of the line, while matching only spaces and tabs.
[user]
I’m using Python. What would the actual code look like, including a small test and a way to display the result clearly?
[assistant]
Python’s `re` module supports multiline mode with `re.MULTILINE`. Here is a self-contained example:
```python
import re
text = (
"INFO started \n"
"DEBUG value=42\t\t\n"
"NOTICE keep internal spaces \n"
"\n"
" \n"
"\t\t\n"
"INFO finished\n"
)
cleaned = re.sub(r"[ \t]+$", "", text, flags=re.MULTILINE)
print("Input repr: ")
print(repr(text))
print("\nOutput repr:")
print(repr(cleaned))
print("\nOutput lines:")
for number, line in enumerate(cleaned.splitlines(), start=1):
print(f"{number}: {line!r}")
```
Typical output is:
```text
Output lines:
1: 'INFO started'
2: 'DEBUG value=42'
3: 'NOTICE keep internal spaces'
4: ''
5: ''
6: ''
7: 'INFO finished'
```
The `repr()` calls expose tabs, newlines, and empty strings instead of rendering them invisibly.
[user]
Could you include CRLF input too? I want the line endings preserved exactly, and I’d prefer not to accidentally remove a carriage return.
[assistant]
For mixed or CRLF input, use a lookahead so the match stops before either `\n` or `\r\n`:
```python
import re
text = (
"INFO started \r\n"
"DEBUG value=42\t\t\r\n"
"NOTICE keep internal spaces \r\n"
"\r\n"
" \r\n"
"\t\t\r\n"
"INFO finished\r\n"
)
cleaned = re.sub(r"[ \t]+(?=\r?$)", "", text, flags=re.MULTILINE)
print("Input:")
print(repr(text))
print("\nOutput:")
print(repr(cleaned))
print("\nLine endings and content:")
for number, line in enumerate(cleaned.splitlines(keepends=True), start=1):
ending = ""
if line.endswith("\r\n"):
content, ending = line[:-2], "\\r\\n"
elif line.endswith("\n"):
content, ending = line[:-1], "\\n"
elif line.endswith("\r"):
content, ending = line[:-1], "\\r"
else:
content = line
print(f"{number}: content={content!r}, ending={ending!r}")
```
The important part is `(?=\r?$)`: it asserts that the spaces or tabs are immediately followed by an optional carriage return and the line end. The replacement therefore cannot consume `\r` or `\n`.
[user]
How should I handle a file that might have LF, CRLF, or old-style CR line endings? I’d like one Python function, and I want whitespace-only lines to remain unchanged.
[assistant]
Use an explicit line-ending alternation and require a non-whitespace character before the trailing spaces or tabs. This preserves line endings and leaves whitespace-only lines alone:
```python
import re
_TRAILING = re.compile(r"(?<=\S)[ \t]*(?=\r\n|\r|\n|$)")
def strip_trailing_space(text: str) -> str:
return _TRAILING.sub("", text)
sample = (
"LF line \n"
"CRLF line\t\t\r\n"
"CR line \r"
" \n"
"\t\t\r\n"
"mixed \r"
"final line "
)
result = strip_trailing_space(sample)
print("Input: ")
print(repr(sample))
print("Output:")
print(repr(result))
```
The lookbehind `(?<=\S)` requires the character immediately before the matched spaces or tabs to be non-whitespace. Consequently, a whitespace-only line is not changed. The lookahead recognizes LF, CRLF, CR, or end-of-string, and the replacement removes only the trailing horizontal whitespace.
[user]
Does that pattern work correctly when a line ends with other whitespace, such as a nonbreaking space? I only need ordinary spaces and tabs removed, but I want the behavior explained.
[assistant]
Yes, with one detail: `\S` means “not whitespace” according to Python’s Unicode rules. If a line ends with a nonbreaking space, the `[ \t]*` part does not remove it, because it matches only U+0020 space and tab. However, the `(?<=\S)` condition can prevent an immediately preceding ordinary space or tab from matching if the character before it is itself Unicode whitespace.
For strictly predictable behavior, define the valid preceding character as anything except an ordinary space, tab, or line ending:
```python
import re
_TRAILING = re.compile(r"(?<=[^ \t\r\n])[ \t]*(?=\r\n|\r|\n|$)")
```
Examples:
```python
tests = [
"ordinary \n",
"tabs\t\t\r\n",
"nonbreaking\u00a0 \n",
"only-nonbreaking\u00a0\n",
"spaces-only \n",
]
for value in tests:
print(repr(value), "=>", repr(_TRAILING.sub("", value)))
```
Expected results:
```text
'ordinary \n' => 'ordinary\n'
'tabs\t\t\r\n' => 'tabs\r\n'
'nonbreaking\xa0 \n' => 'nonbreaking\xa0\n'
'only-nonbreaking\xa0\n' => 'only-nonbreaking\xa0\n'
'spaces-only \n' => 'spaces-only\n'
```
That last case contains ordinary text followed by spaces, so it is cleaned. A line made exclusively of spaces or tabs does not match because there is no preceding non-space/tab character.
[user]
I noticed that your last “spaces-only” example has text in it. Could you correct the tests and include a no-final-newline case?
[assistant]
You’re right; that label was inaccurate. Here is a corrected test set using the strict pattern:
```python
import re
_TRAILING = re.compile(r"(?<=[^ \t\r\n])[ \t]*(?=\r\n|\r|\n|$)")
tests = [
("ordinary spaces", "ordinary \n"),
("tabs", "tabs\t\t\r\n"),
("nonbreaking plus spaces", "nonbreaking\u00a0 \n"),
("only nonbreaking space", "\u00a0\n"),
("spaces only", " \n"),
("tabs only", "\t\t\r\n"),
("no final newline", "final line "),
]
for label, value in tests:
cleaned = _TRAILING.sub("", value)
print(f"{label}:")
print(f" input: {value!r}")
print(f" output: {cleaned!r}")
```
Expected output:
```text
ordinary spaces:
input: 'ordinary \n'
output: 'ordinary\n'
tabs:
input: 'tabs\t\t\r\n'
output: 'tabs\r\n'
nonbreaking plus spaces:
input: 'nonbreaking\xa0 \n'
output: 'nonbreaking\xa0\n'
only nonbreaking space:
input: '\xa0\n'
output: '\xa0\n'
spaces only:
input: ' \n'
output: ' \n'
tabs only:
input: '\t\t\r\n'
output: '\t\t\r\n'
no final newline:
input: 'final line '
output: 'final line'
```
The final item is cleaned because the lookahead also permits end-of-string. No line ending is consumed in any case.
[user]
Can this be done without lookbehind? I may use an engine where lookbehind support is limited, but I still want whitespace-only lines preserved.
[assistant]
Yes. Capture the content before the trailing spaces or tabs, then replace the whole match with that capture. To support LF, CRLF, CR, and a final line without a terminator, use:
```regex
([^ \t\r\n])([ \t]*)(?=\r\n|\r|\n|$)
```
Replace with:
```text
\1
```
In Python:
```python
import re
pattern = re.compile(r"([^ \t\r\n])([ \t]*)(?=\r\n|\r|\n|$)")
def strip_trailing_space(text):
return pattern.sub(r"\1", text)
```
The first capture must be a non-space, non-tab, non-line-ending character. Therefore, a whitespace-only line has no match and remains unchanged. The line ending is only checked by the lookahead, so it is preserved. In replacement APIs that use `$1` rather than `\1`, use the syntax required by that engine.
[user]
I’m also working with a tool that has no lookahead. Is there a practical fallback, and what limitations should I expect?
[assistant]
A common fallback is to match each complete line and capture the content plus its line ending, then replace the line with the captured content after removing trailing spaces or tabs in a second step. If the tool supports alternation but neither lookaround, this single-pass pattern can work for ordinary lines:
```regex
([^ \t\r\n](?:[^ \r\n]*[^ \t\r\n])?)[ \t]*(\r\n|\r|\n|$)
```
Replace with:
```text
\1\2
```
It preserves the line ending and does not match whitespace-only lines. However, the pattern is more complicated and may behave differently in engines where `$` can match before a final newline or where captures in optional groups are handled unusually.
The most portable approach is usually two passes:
1. Split the input into lines while retaining each line ending.
2. For each line, remove only a final run matching:
```regex
[ \t]+$
```
3. Leave the line unchanged when its content consists only of spaces or tabs.
Pseudocode:
```text
for each line including its ending:
separate content from ending
if content contains a non-space/tab:
content = replace final [ \t]+ with empty
output content + ending
```
Without lookahead or lookbehind, preserving several line-ending styles and distinguishing whitespace-only lines generally requires either captures with careful grouping or line-by-line processing.