Regular expressions: Difference between revisions

no edit summary
m (→‎{{header|Raku}}: 'use v6' obsolete)
No edit summary
Line 698:
<pre>hello hello! world world!
hello HELLO world WORLD</pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.RegularExpressions}}
Sample program that uses a regex, for translate a line of code in cpp to pascal.
<lang Delphi>
program Regular_expressions;
 
{$APPTYPE CONSOLE}
{$R *.res}
 
uses
System.SysUtils,
System.RegularExpressions;
 
const
CPP_IF = '\s*if\s*\(\s*(?<COND>.*)\s*\)\s*\{\s*return\s+(?<RETURN>.+);\s*\}';
PASCAL_IF = 'If ${COND} then result:= ${RETURN};';
 
var
RegularExpression: TRegEx;
str: string;
 
begin
str := ' if ( a < 0 ) { return -a; }';
 
Writeln('Expression: '#10#10, str);
 
if RegularExpression.Create(CPP_IF).IsMatch(str) then
begin
Writeln(#10' Is a single If in Cpp:'#10);
 
Writeln('Translate to Pascal:'#10);
str := RegularExpression.Create(CPP_IF).Replace(str, PASCAL_IF);
Writeln(str);
end;
readln;
end.
 
</lang>
 
{{out}}
<pre>
Expression:
 
if ( a < 0 ) { return -a; }
 
Is a single If in Cpp:
 
Translate to Pascal:
 
If a < 0 then result:= -a;
</pre>
=={{header|Elixir}}==
Elixir allows pattern matching using the <code>~r</code> sigil.
478

edits