Regular expressions: Difference between revisions

→‎{{header|TXR}}: Strip C comments
(→‎{{header|TXR}}: Strip C comments)
Line 1,231:
=={{header|TXR}}==
{{works with|TXR git head}}
 
===Search and replace: simple===
 
Txr is not designed for sed-like filtering, but here is how to do <code>sed -e 's/dog/cat/g'</code>:
Line 1,245 ⟶ 1,247:
 
To output the substitution, we simply spit out the mismatching texts followed by the replacement text, and then add the suffix.
 
===Search and replace: strip comments from C source===
 
Based on the technique of the previous example, here is a query for stripping C comments from a source file, replacing
them by a space. Here, the "non-greedy" version of the regex Kleene operator is used, denoted by <code>%</code>. This allows for a very simple, straightforward regex which correctly matches C comments. The <code>freeform</code> operator allows the entire input stream to be treated as one big line, so this works across multi-line comments.
 
<lang txr>@(freeform)
@(coll :gap 0)@notcomment@{comment /[/][*].%[*][/]/}@(end)@tail
@(output)
@(rep)@notcomment @(end)@tail
@(end)</lang>
 
=={{header|Vedit macro language}}==
Anonymous user