# If you'd like to omit non-matching lines from the result; add ';d' to the end of the expression.
sed -E 's/(?(DEFINE)
(?<palindrome>
# Recursive alternative first to match recursive palindromes.
# Invert alternatives order to match nested palindromes individually
# and (drastically) reduce backtracking.
(?<l1>\p{L})\p{M}* [\s\p{P}]* (?&palindrome) [\s\p{P}]* \k<l1>\p{M}*
| (?<l2>\p{L})\p{M}* [\s\p{P}]* \k<l2>\p{M}*
| \p{L}\p{M}*
)
)
(?<=[\s\p{P}]|^) (?&palindrome) (?(?=\s*\p{P}) (?:\s*\p{P})+ | (?=\s|$))/[$0]/gix;t' <<< "~ should not match
jambon
~ Simple
a
bb
cc
ddd
bob
ara
abbb a
radar
essayasse
~ Spaces, diacritics and punctuation
Don't nod!Step on no pets.
Ésope reste ici et se repose.
Élu par cette crapule ! ?
Tu l'as trop écrasé, César, ce Port-Salut !
Zeus a été à Suez.
~ Recursive palindromes
a a !
ah ha !
abba / ab b a
~
abba été àb ba
~
abba, ab b a, abbà été àb ba"
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for SED, please visit: https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html