Regular expressions: Difference between revisions

 
(35 intermediate revisions by 18 users not shown)
Line 3:
{{omit from|BASIC}}
{{omit from|Brlcad}}
{{omit from|EasyLang}}
{{omit from|GUISS}}
{{omit from|PARI/GP}}
Line 11 ⟶ 12:
:*   substitute part of a string using a regular expression
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V string = ‘This is a string’
 
I re:‘string$’.search(string)
print(‘Ends with string.’)
 
string = string.replace(re:‘ a ’, ‘ another ’)
print(string)</syntaxhighlight>
 
{{out}}
<pre>
Ends with string.
This is another string
</pre>
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">
"haystack" /a./ r:match . cr
"haystack" /a./ "blah" s:replace! . cr
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 22 ⟶ 40:
hblahstblahk
</pre>
 
=={{header|ABAP}}==
 
<syntaxhighlight lang="abap">
<lang ABAP>
DATA: text TYPE string VALUE 'This is a Test'.
 
Line 36 ⟶ 55:
cl_demo_output=>write( text ).
cl_demo_output=>display( ).
</syntaxhighlight>
</lang>
 
Output:
Line 48 ⟶ 67:
There is no Regular Expression library in the Ada Standard,
so I am using one of the libraries provided by gnat/gcc.
<langsyntaxhighlight lang="ada">with Ada.Text_IO; with Gnat.Regpat; use Ada.Text_IO;
 
procedure Regex is
Line 90 ⟶ 109:
Str := Str(Str'First .. First-1) & "pattern" & Str(Last+1 .. Str'Last);
Put_Line(Str);
end Regex;</langsyntaxhighlight>
 
{{out}}
Line 98 ⟶ 117:
<matching>
I love pattern matching!</pre>
 
=={{header|AppleScript}}==
{{libheader|Satimage.osax}}
<lang applescript>try
find text ".*string$" in "I am a string" with regexp
on error message
return message
end try
 
try
change "original" into "modified" in "I am the original string" with regexp
on error message
return message
end try</lang>
 
{{out}}
<pre>
</pre>
 
 
=={{header|ALGOL 68}}==
Line 124:
<!-- {{does not work with|ALGOL 68|Standard - grep/sub in string are not part of the standard's prelude. }} -->
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<langsyntaxhighlight lang="algol68">INT match=0, no match=1, out of memory error=2, other error=3;
 
STRING str := "i am a string";
Line 136:
# Replace: #
 
IF sub in string(" a ", " another ",str) = match THEN printf(($gl$, str)) FI;</langsyntaxhighlight>
{{out}}
<pre>
Line 152:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
 
For example:<langsyntaxhighlight lang="algol68">FORMAT pattern = $ddd" "c("cats","dogs")$;
FILE file; STRING book; associate(file, book);
on value error(file, (REF FILE f)BOOL: stop);
Line 163:
print(("Dalmatians: ", dalmatians, new line));
count OF dalmatians +:=1;
printf(($"Gives: "$, pattern, dalmatians, $l$))</langsyntaxhighlight>
{{out}}
<pre>
Line 169:
Gives 101 dogs
</pre>
 
=={{header|Amazing Hopper}}==
task 1: match a string against a regular expression (Hopper use POSIX):
<syntaxhighlight lang="amazing hopper">
#include <hopper.h>
 
main:
expReg="[A-Z]{1,2}[0-9][0-9A-Z]? +[0-9][A-Z]{2}"
flag compile = REG_EXTENDED
flag match=0
número de matches=10, T1=0
{flag compile,expReg} reg compile(T1) // compile regular expression, pointed whit T1
{flag match,número de matches,T1,"We are at SN12 7NY for this course"},reg match, // execute
println
reg free(T1) // free pointer to regular expression compiled.
exit(0)
</syntaxhighlight>
{{out}}
<pre>
11 18 SN12 7NY
</pre>
Task 2: Hopper does not substitute using regular expressions, but using proper functions.
<syntaxhighlight lang="amazing hopper">
#include <hopper.h>
 
main:
expReg="[A-Z]{1,2}[0-9][0-9A-Z]? +[0-9][A-Z]{2}"
cadena = "We are at SN12 7NY for this course"
flag compile = REG_EXTENDED
flag match=0
número de matches=10, T1=0
{flag compile,expReg} reg compile(T1) // compile regular expression, pointed whit T1
{flag match,número de matches,T1,cadena},reg match, // execute
matches=0,mov(matches)
reg free(T1) // free pointer to regular expression compiled.
From=0, To=0, toSearch=""
[1,1]get(matches), mov(From)
[1,2]get(matches), mov(To)
[1,3]get(matches), mov(toSearch)
// substitute with "transform":
{"another thing",toSearch,cadena}transform, println
// substitute with "delete"/"insert":
{To}minus(From),plus(1), {From, cadena} delete, mov(cadena)
{From,"another thing",cadena}insert , println
 
exit(0)
</syntaxhighlight>
{{out}}
<pre>
We are at another thing for this course
We are at another thing for this course
</pre>
 
=={{header|AppleScript}}==
{{libheader|Satimage.osax}}
<syntaxhighlight lang="applescript">try
find text ".*string$" in "I am a string" with regexp
on error message
return message
end try
 
try
change "original" into "modified" in "I am the original string" with regexp
on error message
return message
end try</syntaxhighlight>
----
As from macOS 10.14 Mojave, third-party scripting additions (OSAXen) such as the Satimage OSAX are essentially unusable. (One of Apple's increasingly tight security measures.) They ''are'' allowed under very strict conditions as part of an application's own resources and [https://latenightsw.com Late Night Software], the developer of Script Debugger, has released a [https://forum.latenightsw.com/t/use-satimage-scripting-additions-on-catalina-and-mojave/1576 SatimageOSAX] application as a stop-gap measure to allow existing Satimage-dependent scripts to be used with minimal editing until they're rewritten to use other commands.
 
The alternatives at the moment are to use one of the text-editing languages available through the <tt>do shell script</tt> command (AppleScript's own StandardAdditions OSAX does still work) or to use AppleScriptObjectiveC. The scripts below assume it's known that the strings will consist of just one line.
 
'''do shell script:'''
<syntaxhighlight lang="applescript">-- Get the run of non-white-space at the end, if any.
try
set output to (do shell script "echo 'I am a string' | egrep -o '\\S+$'")
on error message
set output to "No match"
end try
-- Replace the first instance of "orig…" with "modified".
set moreOutput to(do shell script "echo 'I am the original string' | sed 's/orig[a-z]*/modified/'")
return output & linefeed & moreOutput</syntaxhighlight>
 
'''ASObjC''' uses [http://userguide.icu-project.org/strings/regexp ICU regex]:
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
-- Get the run of non-white-space at the end, if any.
set aString to current application's class "NSString"'s stringWithString:("I am a string")
set matchRange to aString's rangeOfString:("\\S++$") ¬
options:(current application's NSRegularExpressionSearch) range:({0, aString's |length|()})
if (matchRange's |length|() > 0) then
set output to aString's substringWithRange:(matchRange)
else
set output to "No match"
end if
 
-- Replace the first instance of "orig…" with "modified".
set anotherString to current application's class "NSString"'s stringWithString:("I am the original string")
set matchRange2 to anotherString's rangeOfString:("orig[a-z]*+") ¬
options:(current application's NSRegularExpressionSearch) range:({0, anotherString's |length|()})
if (matchRange2's |length|() > 0) then
set moreOutput to anotherString's stringByReplacingCharactersInRange:(matchRange2) withString:("modified")
else
set moreOutput to anotherString
end if
 
return (output as text) & linefeed & moreOutput</syntaxhighlight>
As an alternative to the NSString regex options used above, there's also a dedicated NSRegularExpression class:
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
-- Get the run of non-white-space at the end, if any.
set aString to current application's class "NSString"'s stringWithString:("I am a string")
set aRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:("\\S++$") options:(0) |error|:(missing value)
set matchRange to aRegex's rangeOfFirstMatchInString:(aString) options:(0) range:({0, aString's |length|()})
if (matchRange's |length|() > 0) then
set output to aString's substringWithRange:(matchRange)
else
set output to "No match"
end if
 
-- Replace the first instance of "orig…" with "modified".
set anotherString to current application's class "NSString"'s stringWithString:("I am the original string")
set anotherRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:("orig[a-z]*+") options:(0) |error|:(missing value)
set matchRange2 to anotherRegex's rangeOfFirstMatchInString:(anotherString) options:(0) range:({0, anotherString's |length|()})
if (matchRange2's |length|() > 0) then
set moreOutput to anotherString's stringByReplacingCharactersInRange:(matchRange2) withString:("modified")
else
set moreOutput to anotherString
end if
 
return (output as text) & linefeed & moreOutput</syntaxhighlight>
 
{{out}}
The latter three scripts all return:
<pre>"string
I am the modified string"</pre>
 
Shane Stanley has released a script library called [http://www.macosxautomation.com/applescript/apps/Script_Libs.html#RegexAndStuffLib RegexAndStuffLib], which is written in ASObjC, but provides less verbose commands for use in client scripts.
 
=={{header|Argile}}==
<langsyntaxhighlight Argilelang="argile">use std, regex
 
(: matching :)
Line 192 ⟶ 338:
uninit regex
 
check mem leak; use dbg (:optional:)</langsyntaxhighlight>
 
(note that it needs to be compiled with argrt library)
Line 201 ⟶ 347:
$0m3 4ll0c4t3d $tr1ng
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">s: "This is a string"
if contains? s {/string$/} -> print "yes, it ends with 'string'"
replace 's {/[as]/} "x"
 
print s</syntaxhighlight>
 
{{out}}
 
<pre>yes, it ends with 'string'
Thix ix x xtring</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % foundpos := RegExMatch("Hello World", "World$")
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")</langsyntaxhighlight>
 
=={{header|AWK}}==
AWK supports regular expressions, which are typically enclosed using slash symbols at the front and back, and the tilde regular expression binding operator:
<langsyntaxhighlight lang="awk">$ awk '{if($0~/[A-Z]/)print "uppercase detected"}'
abc
ABC
uppercase detected</langsyntaxhighlight>
As shorthand, a regular expression in the condition part fires if it matches an input line:
<langsyntaxhighlight lang="awk">awk '/[A-Z]/{print "uppercase detected"}'
def
DeF
uppercase detected</langsyntaxhighlight>
For substitution, the first argument can be a regular expression, while the replacement string is constant (only that '&' in it receives the value of the match):
<langsyntaxhighlight lang="awk">$ awk '{gsub(/[A-Z]/,"*");print}'
abCDefG
ab**ef*
$ awk '{gsub(/[A-Z]/,"(&)");print}'
abCDefGH
ab(C)(D)ef(G)(H)</langsyntaxhighlight>
This variant matches one or more uppercase letters in one round:
<langsyntaxhighlight lang="awk">$ awk '{gsub(/[A-Z]+/,"(&)");print}'
abCDefGH
ab(CD)ef(GH)</langsyntaxhighlight>
 
Regular expression negation can be achieved by combining the regular expression binding operator with a logical not operator, as follows:
Line 238 ⟶ 398:
{{works with|BBC BASIC for Windows}}
Uses the [http://people.delphiforums.com/gjc/gnu_regex.html gnu_regex] library.
<langsyntaxhighlight lang="bbcbasic"> SYS "LoadLibrary", "gnu_regex.dll" TO gnu_regex%
IF gnu_regex% = 0 ERROR 100, "Cannot load gnu_regex.dll"
SYS "GetProcAddress", gnu_regex%, "regcomp" TO regcomp
Line 277 ⟶ 437:
ENDIF
 
SYS "FreeLibrary", gnu_regex%</langsyntaxhighlight>
{{out}}
<pre>
Line 296 ⟶ 456:
 
List all rational numbers smaller then 7 hidden in the string "fgsakg789/35768685432fkgha"
<langsyntaxhighlight lang="bracmat">@("fesylk789/35768poq2art":? (#<7:?n & out$!n & ~) ?)</langsyntaxhighlight>
{{out}}
<pre>789/357
Line 321 ⟶ 481:
Test
 
<langsyntaxhighlight lang="brat">str = "I am a string"
 
true? str.match(/string$/)
Line 328 ⟶ 488:
false? str.match(/^You/)
{ p "Does not start with 'You'" }
</syntaxhighlight>
</lang>
 
Substitute
 
<langsyntaxhighlight lang="brat"># Substitute in copy
 
str2 = str.sub(/ a /, " another ")
Line 351 ⟶ 511:
 
p str # prints "I Am Another string"
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Line 358 ⟶ 518:
As far as I can see, POSIX defined function for regex matching, but nothing for substitution. So we must do all the hard work ''by hand''. The complex-appearing code could be turned into a function.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
Line 400 ⟶ 560:
return 0;
}</langsyntaxhighlight>
 
===Alternative using GLib===
The task is a bit easier with GLib's Perl-compatible regular expression functionality.
{{libheader|GLib}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <glib.h>
 
void print_regex_match(const GRegex* regex, const char* string) {
GMatchInfo* match_info;
gboolean match = g_regex_match(regex, string, 0, &match_info);
printf(" string = '%s': %s\n", string, match ? "yes" : "no");
g_match_info_free(match_info);
}
 
void regex_match_demo() {
const char* pattern = "^[a-z]+$";
GError* error = NULL;
GRegex* regex = g_regex_new(pattern, 0, 0, &error);
if (regex == NULL) {
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
return;
}
printf("Does the string match the pattern '%s'?\n", pattern);
print_regex_match(regex, "test");
print_regex_match(regex, "Test");
g_regex_unref(regex);
}
 
void regex_replace_demo() {
const char* pattern = "[0-9]";
const char* input = "Test2";
const char* replace = "X";
GError* error = NULL;
GRegex* regex = g_regex_new(pattern, 0, 0, &error);
if (regex == NULL) {
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
return;
}
char* result = g_regex_replace_literal(regex, input, -1,
0, replace, 0, &error);
if (result == NULL) {
fprintf(stderr, "%s\n", error->message);
g_error_free(error);
} else {
printf("Replace pattern '%s' in string '%s' by '%s': '%s'\n",
pattern, input, replace, result);
g_free(result);
}
g_regex_unref(regex);
}
 
int main() {
regex_match_demo();
regex_replace_demo();
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Does the string match the pattern '^[a-z]+$'?
string = 'test': yes
string = 'Test': no
Replace pattern '[0-9]' in string 'Test2' by 'X': 'TestX'
</pre>
 
=={{header|C sharp}}==
<syntaxhighlight lang="csharp">using System;
using System.Text.RegularExpressions;
 
class Program {
static void Main(string[] args) {
string str = "I am a string";
 
if (new Regex("string$").IsMatch(str)) {
Console.WriteLine("Ends with string.");
}
 
str = new Regex(" a ").Replace(str, " another ");
Console.WriteLine(str);
}
}</syntaxhighlight>
 
=={{header|C++}}==
{{works with|g++|4.0.2 (may need to be retested?)}}
Standards earlier than C++11 can make use of Boost's Regex library via boost/regex.hpp
 
<syntaxhighlight lang="cpp">#include <iostream>
{{libheader|Boost}}
<lang cpp>#include <iostream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
 
int main()
{
booststd::regex re(".* string$");
std::string s = "Hi, I am a string";
 
// match the complete string
if (booststd::regex_match(s, re))
std::cout << "The string matches.\n";
else
Line 423 ⟶ 666:
 
// match a substring
booststd::regex re2(" a.*a");
booststd::smatch match;
if (booststd::regex_search(s, match, re2))
{
std::cout << "Matched " << match.length()
Line 439 ⟶ 682:
// replace a substring
std::string dest_string;
booststd::regex_replace(std::back_inserter(dest_string),
s.begin(), s.end(),
re2,
"'m now a changed");
std::cout << dest_string << std::endl;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Text.RegularExpressions;
 
class Program {
static void Main(string[] args) {
string str = "I am a string";
 
if (new Regex("string$").IsMatch(str)) {
Console.WriteLine("Ends with string.");
}
 
str = new Regex(" a ").Replace(str, " another ");
Console.WriteLine(str);
}
}</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(let [s "I am a string"]
;; match
(when (re-find #"string$" s)
Line 473 ⟶ 699:
;; substitute
(println (clojure.string/replace s " a " " another "))
)</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 480 ⟶ 706:
Uses [http://weitz.de/cl-ppcre/ CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp].
 
<langsyntaxhighlight lang="lisp">(let ((string "I am a string"))
(when (cl-ppcre:scan "string$" string)
(write-line "Ends with string"))
(unless (cl-ppcre:scan "^You" string )
(write-line "Does not start with 'You'")))</langsyntaxhighlight>
 
Substitute
 
<langsyntaxhighlight lang="lisp">(let* ((string "I am a string")
(string (cl-ppcre:regex-replace " a " string " another ")))
(write-line string))</langsyntaxhighlight>
 
Test and Substitute
 
<langsyntaxhighlight lang="lisp">(let ((string "I am a string"))
(multiple-value-bind (string matchp)
(cl-ppcre:regex-replace "\\bam\\b" string "was")
(when matchp
(write-line "I was able to find and replace 'am' with 'was'."))))</langsyntaxhighlight>
 
===CLISP regexp engine===
{{works with|CLISP}}
[[Clisp]] comes with [http://clisp.org/impnotes/regexp-mod.html built-in regexp matcher]. On a Clisp prompt:
<langsyntaxhighlight lang="lisp">[1]> (regexp:match "fox" "quick fox jumps")
#S(REGEXP:MATCH :START 6 :END 9)</langsyntaxhighlight>
To find all matches, loop with different <code>:start</code> keyword.
 
Replacing text can be done with the help of <code>REGEXP:REGEXP-SPLIT</code> function:
<langsyntaxhighlight lang="lisp">[2]> (defun regexp-replace (pat repl string)
(reduce #'(lambda (x y) (string-concat x repl y))
(regexp:regexp-split pat string)))
REGEXP-REPLACE
[3]> (regexp-replace "x\\b" "-X-" "quick foxx jumps")
"quick fox-X- jumps"</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.regex;
 
Line 527 ⟶ 753:
// Substitute.
s.replace(" a ".regex, " another ").writeln;
}</langsyntaxhighlight>
{{out}}
<pre>Ends with 'string'.
Line 534 ⟶ 760:
 
=={{header|Dart}}==
<langsyntaxhighlight lang="d">RegExp regexp = new RegExp(r'\w+\!');
 
String capitalize(Match m) => '${m[0].substring(0, m[0].length-1).toUpperCase()}';
Line 543 ⟶ 769:
print(hello);
print(hellomodified);
}</langsyntaxhighlight>
{{out}}
<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.
<syntaxhighlight 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.
 
</syntaxhighlight>
 
{{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.
<syntaxhighlight lang="elixir">
<lang Elixir>
str = "This is a string"
if str =~ ~r/string$/, do: IO.inspect "str ends with 'string'"
</syntaxhighlight>
</lang>
A number of modifiers can be appended to the regular expression; <code>~r/pattern/i</code>, for instance, toggles case insensitivity.
<syntaxhighlight lang="elixir">
<lang Elixir>
str =~ ~r/this/ # => false
str =~ ~r/this/i # => true
</syntaxhighlight>
</lang>
Both <code>Regex</code> and <code>String</code> have a <code>replace</code> function.
<syntaxhighlight lang="elixir">
<lang Elixir>
str1 = ~r/a/ |> Regex.replace(str,"another")
str2 = str1 |> String.replace(~r/another/,"even another")
</syntaxhighlight>
</lang>
<code>Regex.replace</code> allows for a function to be used as a replacement value. A function can modify the found pattern.
<syntaxhighlight lang="elixir">
<lang Elixir>
str3 = ~r/another/ |> Regex.replace(str2, fn x -> "#{String.upcase(x)}" end)
</syntaxhighlight>
</lang>
 
{{out}}
Line 576 ⟶ 853:
"This is even another string"<br>
"This is even ANOTHER string"<br>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(let ((string "I am a string"))
<lang Emacs Lisp>
(defunwhen (string-match-p (word"string$" strstring)
(setqmessage pos"Ends (with 'string-match word str'") )
(message "%s" (replace-regexp-in-string " a " " another " string)))</syntaxhighlight>
(if pos
(progn
(insert (format "%s found at position %d in: %s\n" word pos str) )
(setq regex (format "^.+%s" word) )
(setq str (replace-regexp-in-string regex (format "left %s" word) str) )
(setq regex (format "%s.+$" word) )
(setq str (replace-regexp-in-string regex (format "%s right" word) str) )
(insert (format "result: %s\n" str) ))
(insert (format "%s not found in: %s\n" word str) )))
 
{{out}}
(setq str1 "before center after" str2 "before centre after")
 
Ends with 'string'
(progn
I am another string
(match "center" str1)
(insert "\n")
(match "center" str2) )
</lang>
<b>Output:</b>
<pre>
center found at position 7 in: before center after
result: left center right
 
center not found in: before centre after
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">match() ->
String = "This is a string",
case re:run(String, "string$") of
Line 616 ⟶ 876:
String = "This is a string",
NewString = re:replace(String, " a ", " another ", [{return, list}]),
io:format("~s~n",[NewString]).</langsyntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
open System.Text.RegularExpressions
 
Line 631 ⟶ 890:
let rstr = Regex(" a ").Replace(str, " another ")
Console.WriteLine(rstr)
0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel prettyprint regexp ;
IN: rosetta-code.regexp
 
Line 641 ⟶ 900:
"1001" R/ 10+/ re-contains? . ! Does the string contain the regexp anywhere?
 
"blueberry pie" R/ \p{alpha}+berry/ "pumpkin" re-replace print</langsyntaxhighlight>
{{out}}
<pre>
Line 653 ⟶ 912:
{{libheader|Forth Foundation Library}}
Test/Match
<langsyntaxhighlight lang="forth">include ffl/rgx.fs
 
\ Create a regular expression variable 'exp' in the dictionary
Line 671 ⟶ 930:
[ELSE]
.( No match.) cr
[THEN]</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Dim As String text = "I am a text"
If Right(text, 4) = "text" Then
Print "'" + text + "' ends with 'text'"
End If
 
Dim As Integer i = Instr(text, "am")
text = Left(text, i - 1) + "was" + Mid(text, i + 2)
Print "replace 'am' with 'was' = " + text
Sleep
</syntaxhighlight>
{{out}}
<pre>'I am a text' ends with 'text'
replace 'am' with 'was' = I was a text</pre>
 
 
=={{header|Frink}}==
Pattern matching:
<langsyntaxhighlight lang="frink">
line = "My name is Inigo Montoya."
 
Line 684 ⟶ 960:
println["Last name is: $last"]
}
</syntaxhighlight>
</lang>
 
Replacement: (Replaces in the variable <code>line</code>)
<langsyntaxhighlight lang="frink">
line =~ %s/Frank/Frink/g
</syntaxhighlight>
</lang>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=5a2a1052f8dff12596fa7f45242d25a9 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String = "Hello world!"
 
Line 703 ⟶ 979:
Print sString
 
End </langsyntaxhighlight>
Output:
<pre>
Line 715 ⟶ 991:
<br />
Replacement:<br />
<langsyntaxhighlight lang="genexus">&string = &string.ReplaceRegEx("^\s+|\s+$", "") // it's a trim!
&string = &string.ReplaceRegEx("Another (Match)", "Replacing $1") // Using replace groups</langsyntaxhighlight>
Check match:
<langsyntaxhighlight lang="genexus">If (&string.IsMatch("regex$"))
// The string ends with "regex"
EndIf</langsyntaxhighlight>
Split RegEx:
<langsyntaxhighlight lang="genexus">&stringCollection = &string.SplitRegEx("^\d{2,4}")</langsyntaxhighlight>
Matches:
<langsyntaxhighlight lang="genexus">&RegExMatchCollection = &string.Matches("(pa)tt(ern)")
For &RegExMatch In &RegExMatchCollection
&FullMatch = &RegExMatch.Value // &FullMatch contains the full pattern match: "pattern"
Line 730 ⟶ 1,006:
// &matchVarchar contains group matches: "pa", "ern"
EndFor
EndFor</langsyntaxhighlight>
Flags: <br />
s - Dot matches all (including newline) <br />
Line 737 ⟶ 1,013:
Using Flags Sintax: (?flags)pattern <br />
Example:<br />
<langsyntaxhighlight lang="genexus">&string = &string.ReplaceRegEx("(?si)IgnoreCase.+$", "") // Flags s and i</langsyntaxhighlight>
Error Handling:
<langsyntaxhighlight lang="genexus">&string = "abc"
&RegExMatchCollection = &string.Matches("[z-a]") // invalid pattern: z-a
&errCode = RegEx.GetLastErrCode() // returns 0 if no error and 1 if an error has occured
&errDsc = RegEx.GetLastErrDescription()</langsyntaxhighlight>
 
=={{header|Genie}}==
<langsyntaxhighlight lang="genie">[indent=4]
/* Regular expressions, in Genie */
 
Line 761 ⟶ 1,037:
 
except err:RegexError
print err.message</langsyntaxhighlight>
 
{{out}}
Line 770 ⟶ 1,046:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
import "fmt"
import "regexp"
Line 785 ⟶ 1,061:
result := pattern.ReplaceAllString(str, "modified")
fmt.Println(result)
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
"Matching" Solution (it's complicated):
<langsyntaxhighlight lang="groovy">import java.util.regex.*;
 
def woodchuck = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
Line 842 ⟶ 1,118:
 
println ("'${woodchuck}' ${wwNotMatches ? 'does not' : 'does'} match '${woodRE}' exactly")
println ("'${woodchuck}' ${wwMatches ? 'does' : 'does not'} match '${containsWoodRE}' exactly")</langsyntaxhighlight>
 
{{out}}
Line 862 ⟶ 1,138:
 
Replacement Solution (String.replaceAll()):
<langsyntaxhighlight lang="groovy">println woodchuck.replaceAll(/c\w+k/, "CHUCK")</langsyntaxhighlight>
 
{{out}}
Line 868 ⟶ 1,144:
 
Reusable Replacement Solution (Matcher.replaceAll()):
<langsyntaxhighlight lang="groovy">def ck = (woodchuck =~ /c\w+k/)
println (ck.replaceAll("CHUCK"))
println (ck.replaceAll("wind"))
Line 878 ⟶ 1,154:
println (ck.replaceAll("man"))
println (ck.replaceAll("work"))
println (ck.replaceAll("pickle"))</langsyntaxhighlight>
 
{{out}}
Line 894 ⟶ 1,170:
=={{header|Haskell}}==
Test
<langsyntaxhighlight lang="haskell">import Text.Regex
 
str = "I am a string"
Line 900 ⟶ 1,176:
case matchRegex (mkRegex ".*string$") str of
Just _ -> putStrLn $ "ends with 'string'"
Nothing -> return ()</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="haskell">import Text.Regex
 
orig = "I am the original string"
result = subRegex (mkRegex "original") orig "modified"
putStrLn $ result</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">CHARACTER string*100/ "The quick brown fox jumps over the lazy dog" /
REAL, PARAMETER :: Regex=128, Count=256
 
Line 916 ⟶ 1,192:
 
vocals_changed = EDIT(Text=string, Option=Regex, Right="[aeiou]", RePLaceby='**', DO=LEN(string) ) ! changes 11
WRITE(ClipBoard) string ! Th** q****ck br**wn f**x j**mps **v**r th** l**zy d**g</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 923 ⟶ 1,199:
Additionally, there is a regular expression pattern compiler 'RePat' and other supporting functions and variables.
 
<langsyntaxhighlight Iconlang="icon">procedure main()
 
s := "A simple string"
Line 934 ⟶ 1,210:
end
 
link regexp # link to IPL regexp </langsyntaxhighlight>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/procs/regexp.htm See regexp].
Line 945 ⟶ 1,221:
Inform's regex support is similar to Perl's but with some limitations: angle brackets are used instead of square brackets, there is no multiline mode, several control characters and character classes are omitted, and backtracking is slightly less powerful.
 
<langsyntaxhighlight lang="inform7">let T be indexed text;
let T be "A simple string";
if T matches the regular expression ".*string$", say "ends with string.";
replace the regular expression "simple" in T with "replacement";</langsyntaxhighlight>
 
=={{header|J}}==
Line 954 ⟶ 1,230:
J's regex support is built on top of PCRE.
 
<langsyntaxhighlight lang="j">load'regex' NB. Load regex library
str =: 'I am a string' NB. String used in examples.</langsyntaxhighlight>
 
Matching:
<langsyntaxhighlight lang="j"> '.*string$' rxeq str NB. 1 is true, 0 is false
1</langsyntaxhighlight>
 
Substitution:
<langsyntaxhighlight lang="j"> ('am';'am still') rxrplc str
I am still a string</langsyntaxhighlight>
 
Note: use<syntaxhighlight lang J="j"> open'regex'</langsyntaxhighlight> to read the source code for the library. The comments list 6 main definitions and a dozen utility definitions.
 
=={{header|Java}}==
 
<syntaxhighlight lang="java">
/* match entire string against a pattern */
boolean isNumber = "-1234.567".matches("-?\\d+(?:\\.\\d+)?");
 
/* substitute part of string using a pattern */
String reduceSpaces = "a b c d e f".replaceAll(" +", " ");
</syntaxhighlight>
<syntaxhighlight lang="java">
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
/* group capturing example */
Pattern pattern = Pattern.compile("(?:(https?)://)?([^/]+)/(?:([^#]+)(?:#(.+))?)?");
Matcher matcher = pattern.matcher("https://rosettacode.org/wiki/Regular_expressions#Java");
if (matcher.find()) {
String protocol = matcher.group(1);
String authority = matcher.group(2);
String path = matcher.group(3);
String fragment = matcher.group(4);
}
</syntaxhighlight>
<syntaxhighlight lang="java">
/* split a string using a pattern */
String[] strings = "abc\r\ndef\r\nghi".split("\r\n?");
</syntaxhighlight>
 
<br />
An alternate demonstration
{{works with|Java|1.4+}}
Test
 
<langsyntaxhighlight lang="java">String str = "I am a string";
if (str.matches(".*string")) { // note: matches() tests if the entire string is a match
System.out.println("ends with 'string'");
}</langsyntaxhighlight>
 
To match part of a string, or to process matches:
<langsyntaxhighlight lang="java">import java.util.regex.*;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher(str);
while (m.find()) {
// use m.group() to extract matches
}</langsyntaxhighlight>
 
Substitute
 
<langsyntaxhighlight lang="java">String orig = "I am the original string";
String result = orig.replaceAll("original", "modified");
// result is now "I am the modified string"</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Test/Match
<langsyntaxhighlight lang="javascript">var subject = "Hello world!";
 
// Two different ways to create the RegExp object
Line 1,008 ⟶ 1,313:
// matches[0] == "Hello world"
// matches[1] == "world"
var matches = re_PatternToMatch2.exec(subject);</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="javascript">var subject = "Hello world!";
 
// Perform a string replacement
// newSubject == "Replaced!"
var newSubject = subject.replace(re_PatternToMatch, "Replaced");</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,023 ⟶ 1,328:
 
'''Test''':
<langsyntaxhighlight lang="jq">"I am a string" | test("string$") </langsyntaxhighlight>
yields: true
 
'''Substitutution''':
<langsyntaxhighlight lang="jq">"I am a string" | sub(" a "; " another ")</langsyntaxhighlight>
yields: "I am another string"
 
'''Substitution using capture''':
<langsyntaxhighlight lang="jq">"abc" | sub( "(?<head>^.)(?<tail>.*)"; "\(.head)-\(.tail)")</langsyntaxhighlight>
yields: "a-bc"
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* Regular expressions, in Jsish */
 
var re = /s[ai]mple/;
Line 1,044 ⟶ 1,349:
 
var replaced = sentence.replace(re, "different");
printf("replaced sentence is: %s\n", replaced);</langsyntaxhighlight>
 
{{out}}
Line 1,054 ⟶ 1,359:
{{trans|Perl}}
Julia implements Perl-compatible regular expressions (via the built-in [http://www.pcre.org/ PCRE library]). To test for a match:
<langsyntaxhighlight lang="julia">s = "I am a string"
if ismatch(r"string$", s)
println("'$s' ends with 'string'")
end</langsyntaxhighlight>
To perform replacements:
<langsyntaxhighlight lang="julia">s = "I am a string"
s = replace(s, r" (a|an) ", " another ")</langsyntaxhighlight>
There are many [http://docs.julialang.org/en/latest/manual/strings/#regular-expressions other features] of Julia's regular-expression support, too numerous to list here.
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
Line 1,074 ⟶ 1,379:
val s2 = s1.replace(r2, s3)
if (s2 != s1) println("`$s2` replaces `$r2` with `$s3`")
}</langsyntaxhighlight>
 
{{out}}
Line 1,082 ⟶ 1,387:
</pre>
 
=={{header|Langurlangur}}==
The following examples use re2 regex literals.
Langur uses semi-integreted regex. An re2 literal without interpreting langur escape codes uses RE// and with langur escape codes interpreted uses re//. Langur escape codes, when applicable, are interpreted before a string is sent to the regex compiler.
 
There are several functions that can be used with regexes, such as match(), replace(), split(), etc. They can also be matched using the forward operator (->).
Valid quote mark pairs include the following.
// &#39;&#39; "" {} [] () &lt;&gt;
 
To match a string, ...
There are several functions that can be used with regexes, such as match(), replace(), split(), etc.
<syntaxhighlight lang="langur">if "somestring" -> re/abc/ { ... }</syntaxhighlight>
 
Or...
Progressive matching (a.k.a. "global") is done with split() and replace() and the functions that use the plural form, such as matches(), indices(), etc. These will return an array or array of arrays. Progressive matching can be limited by passing a maximum.
<syntaxhighlight lang="langur">if val .x, .y = submatch(re/(abc+).+?(def)/, "somestring") { ... }</syntaxhighlight>
 
Or...
Regex functions that return an array will return an empty array for no match. The matching() function returns a Boolean. The match() function returns a string (or null for no match).
<syntaxhighlight lang="langur">switch "somestring" {
 
To match a string, case -> re/abc/: ...
...
<lang Langur>if matching(re/abc/, "somestring") { ... }</lang>
}</syntaxhighlight>
 
Or...
<syntaxhighlight lang="langur">switch -> re/abc/ {
<lang Langur>if val (.x, .y) = submatch(re/(abc+).+?/, "somestring") { ... }</lang>
case "somestring": ...
...
}</syntaxhighlight>
 
Substitution does not alter the original string.
<langsyntaxhighlight Langurlang="langur">replace("abcdef", re/abc/, "Y")
# result: "Ydef"</langsyntaxhighlight>
 
=={{header|Lasso}}==
Lasso has built in support for regular expressions using ICU regexps.
<langsyntaxhighlight Lassolang="lasso">local(mytext = 'My name is: Stone, Rosetta
My name is: Hippo, Campus
')
Line 1,127 ⟶ 1,437:
#regexp -> reset(-input = #mytext)
'<br />'
#regexp -> replaceall</langsyntaxhighlight>
<pre>Rosetta
Campus
Line 1,137 ⟶ 1,447:
In Lua many string manipulation methods use ''patterns'', which offer almost the same fucntionality as regular expressions, but whose syntax differs slightly. The percent sign (<code>%</code>) is generally used instead of a backslash to start a character class or a reference for a match in a substitution.
 
<langsyntaxhighlight lang="lua">test = "My name is Lua."
pattern = ".*name is (%a*).*"
 
Line 1,145 ⟶ 1,455:
 
sub, num_matches = test:gsub(pattern, "Hello, %1!")
print(sub)</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,159 ⟶ 1,469:
Enumerators for some COM objects are number of function -4&, we can use this in place of string for the name of property.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
declare global ObjRegEx "VBscript.RegExp"
Line 1,233 ⟶ 1,543:
}
Internal
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">regexp(`GNUs not Unix', `\<[a-z]\w+')
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')</langsyntaxhighlight>
 
{{out}}
Line 1,246 ⟶ 1,556:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">#Examples from Maple Help
StringTools:-RegMatch("^ab+bc$", "abbbbc");
StringTools:-RegMatch("^ab+bc$", "abbbbcx");
StringTools:-RegSub("a([bc]*)(c*d)", "abcd", "&-\\1-\\2");
StringTools:-RegSub("(.*)c(anad[ai])(.*)", "Maple is canadian", "\\1C\\2\\3");</langsyntaxhighlight>
{{Out|Output}}
<pre>true
Line 1,257 ⟶ 1,567:
"Maple is Canadian"</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">StringCases["I am a string with the number 18374 in me",RegularExpression["[0-9]+"]]
<lang Mathematica>
StringCasesStringReplace["I am a string with the number 18374 in me",RegularExpression["[0I\\sam"] -9]+> "I'm"]]</syntaxhighlight>
StringReplace["I am a string",RegularExpression["I\\sam"] -> "I'm"]
</lang>
The in-notebook output, in order:
<pre>{18374}
I'm a string</pre>
{18374}
 
I'm a string
</pre>
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
samples = #("Some string 123","Example text 123","string",\
"ThisString Will Not Match","A123,333,string","123451")
Line 1,308 ⟶ 1,615:
)
)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,330 ⟶ 1,637:
 
=={{header|MIRC Scripting Language}}==
<langsyntaxhighlight lang="mirc">alias regular_expressions {
var %string = This is a string
var %re = string$
Line 1,342 ⟶ 1,649:
%re = \b(another)\b
echo -a Result 2: $regsubex(%string,%re,yet \1)
}</langsyntaxhighlight>
 
{{out}}
Line 1,352 ⟶ 1,659:
 
=={{header|MUMPS}}==
<p>MUMPS doesn't have a replacement functionality when using the pattern matching operator, ?. We can mimic it with $PIECE, but $PIECE doesn't work with regular expressions as an operand.</p><langsyntaxhighlight MUMPSlang="mumps">REGEXP
NEW HI,W,PATTERN,BOOLEAN
SET HI="Hello, world!",W="world"
Line 1,364 ⟶ 1,671:
SET BOOLEAN=$FIND(HI,W)
IF BOOLEAN>0 WRITE $PIECE(HI,W,1)_"string"_$PIECE(HI,W,2)
QUIT</langsyntaxhighlight>
Usage:<pre>
USER>D REGEXP^ROSETTA
Line 1,374 ⟶ 1,681:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,411 ⟶ 1,718:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,428 ⟶ 1,735:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp NewLISP ">(regex "[bB]+" "AbBBbABbBAAAA") -> ("bBBb" 1 4)</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import re
 
var s = "This is a string"
Line 1,439 ⟶ 1,746:
 
s = s.replace(re"\ a\ ", " another ")
echo s</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
use RegEx;
 
Line 1,460 ⟶ 1,767:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
Line 1,466 ⟶ 1,773:
{{works with|Mac OS X|10.4+}}
{{works with|iOS|3.0+}}
<langsyntaxhighlight lang="objc">NSString *str = @"I am a string";
NSString *regex = @".*string$";
 
Line 1,474 ⟶ 1,781:
if ([pred evaluateWithObject:str]) {
NSLog(@"ends with 'string'");
}</langsyntaxhighlight>
Unfortunately this method cannot find the location of the match or do substitution.
 
Line 1,481 ⟶ 1,788:
{{works with|Mac OS X|10.7+}}
{{works with|iOS|3.2+}}
<langsyntaxhighlight lang="objc">NSString *str = @"I am a string";
if ([str rangeOfString:@"string$" options:NSRegularExpressionSearch].location != NSNotFound) {
NSLog(@"Ends with 'string'");
}</langsyntaxhighlight>
 
Substitute
{{works with|Mac OS X|10.7+}}
{{works with|iOS|4.0+}} undocumented
<langsyntaxhighlight lang="objc">NSString *orig = @"I am the original string";
NSString *result = [orig stringByReplacingOccurrencesOfString:@"original"
withString:@"modified"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [orig length])];
NSLog(@"%@", result);</langsyntaxhighlight>
 
===NSRegularExpression===
Line 1,500 ⟶ 1,807:
{{works with|iOS|4.0+}}
Test
<langsyntaxhighlight lang="objc">NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"string$"
options:0
error:NULL];
Line 1,509 ⟶ 1,816:
].location != NSNotFound) {
NSLog(@"Ends with 'string'");
}</langsyntaxhighlight>
 
Loop through matches
<langsyntaxhighlight lang="objc">for (NSTextCheckingResult *match in [regex matchesInString:str
options:0
range:NSMakeRange(0, [str length])
Line 1,518 ⟶ 1,825:
// match.range gives the range of the whole match
// [match rangeAtIndex:i] gives the range of the i'th capture group (starting from 1)
}</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="objc">NSString *orig = @"I am the original string";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"original"
options:0
Line 1,529 ⟶ 1,836:
range:NSMakeRange(0, [orig length])
withTemplate:@"modified"];
NSLog(@"%@", result);</langsyntaxhighlight>
 
=={{header|OCaml}}==
=== With the standard library ===
Test
<langsyntaxhighlight lang="ocaml">#load "str.cma";;
let str = "I am a string";;
try
Line 1,540 ⟶ 1,847:
print_endline "ends with 'string'"
with Not_found -> ()
;;</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="ocaml">#load "str.cma";;
let orig = "I am the original string";;
let result = Str.global_replace (Str.regexp "original") "modified" orig;;
(* result is now "I am the modified string" *)</langsyntaxhighlight>
 
=== Using Pcre ===
'''Library:''' [http://ocaml.info/home/ocaml_sources.html#pcre-ocaml ocaml-pcre]
 
<langsyntaxhighlight lang="ocaml">let matched pat str =
try ignore(Pcre.exec ~pat str); (true)
with Not_found -> (false)
Line 1,560 ⟶ 1,867:
Printf.printf "Substitute: %s\n"
(Pcre.replace ~pat:"original" ~templ:"modified" "I am the original string")
;;</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
; matching:
(define regex (string->regex "m/aa(bb|cc)dd/"))
Line 1,576 ⟶ 1,883:
(print (regex "aabcddx")) ; => false
 
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
/* Using the RxRegExp Regular Expression built-in utility class */
 
Line 1,621 ⟶ 1,928:
 
::requires "rxregexp.cls"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,631 ⟶ 1,938:
 
=={{header|Oxygene}}==
<langsyntaxhighlight lang="oxygene">
// Match and Replace part of a string using a Regular Expression
//
Line 1,662 ⟶ 1,969:
end.
</syntaxhighlight>
</lang>
Produces:
<pre>
Line 1,670 ⟶ 1,977:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
[Regex] = {Module.link ['x-oz://contrib/regex']}
String = "This is a string"
Line 1,677 ⟶ 1,984:
{System.showInfo "Ends with string."}
end
{System.showInfo {Regex.replace String " a " fun {$ _ _} " another " end}}</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">
// Match and Replace part of a string using a Regular Expression
//
Line 1,705 ⟶ 2,013:
if r.Exec(myResult) then writeln(' contains ' + r.Match[0]);
end.
</syntaxhighlight>
</lang>
Produces:
<pre>
Line 1,716 ⟶ 2,024:
{{works with|Perl|5.8.8}}
Test
<langsyntaxhighlight lang="perl">$string = "I am a string";
if ($string =~ /string$/) {
print "Ends with 'string'\n";
Line 1,723 ⟶ 2,031:
if ($string !~ /^You/) {
print "Does not start with 'You'\n";
}</langsyntaxhighlight>
 
 
Substitute
<langsyntaxhighlight lang="perl">$string = "I am a string";
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"
print $string;</langsyntaxhighlight>
 
In Perl 5.14+, you can return a new substituted string without altering the original string:
<langsyntaxhighlight lang="perl">$string = "I am a string";
$string2 = $string =~ s/ a / another /r; # $string2 == "I am another string", $string is unaltered
print $string2;</langsyntaxhighlight>
 
 
Test and Substitute
<langsyntaxhighlight lang="perl">$string = "I am a string";
if ($string =~ s/\bam\b/was/) { # \b is a word border
print "I was able to find and replace 'am' with 'was'\n";
}</langsyntaxhighlight>
 
 
Options
<langsyntaxhighlight lang="perl"># add the following just after the last / for additional control
# g = globally (match as many as possible)
# i = case-insensitive
Line 1,751 ⟶ 2,059:
# m = multi-line (the expression is run on each line individually)
 
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung"</langsyntaxhighlight>
 
Omission of the regular expression binding operators
Line 1,757 ⟶ 2,065:
If regular expression matches are being made against the topic variable, it is possible to omit the regular expression binding operators:
 
<langsyntaxhighlight lang="perl">$_ = "I like banana milkshake.";
if (/banana/) { # The regular expression binding operator is omitted
print "Match found\n";
}</langsyntaxhighlight>
 
=={{header|Perl 6}}==
<lang perl6>use v6;
if 'a long string' ~~ /string$/ {
say "It ends with 'string'";
}
 
# substitution has a few nifty features
 
$_ = 'The quick Brown fox';
s:g:samecase/\w+/xxx/;
.say;
# output:
# Xxx xxx Xxx xxx
</lang>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include builtins\regex.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string s = "I am a string"
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">regex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
printf(1,"\"%s\" %s with string\n",{s,iff(length(regex(`string$`,s))?"ends":"does not end")})
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (needs some recent bugfixes to regex.e for p2js)</span>
printf(1,"\"%s\" %s with You\n",{s,iff(length(regex(`^You`,s))?"starts":"does not start")})
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"I am a string"</span>
?gsub(`[A-Z]`,"abCDefG","*")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\"%s\" %s with string\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">regex</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`string$`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))?</span><span style="color: #008000;">"ends"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"does not end"</span><span style="color: #0000FF;">)})</span>
?gsub(`[A-Z]`,"abCDefGH","(&)")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\"%s\" %s with You\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">regex</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`^You`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))?</span><span style="color: #008000;">"starts"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"does not start"</span><span style="color: #0000FF;">)})</span>
?gsub(`[A-Z]+`,"abCDefGH","(&)")
<span style="color: #0000FF;">?</span><span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`[A-Z]`</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abCDefG"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">)</span>
?gsub(`string`,s,"replacement")
<span style="color: #0000FF;">?</span><span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`[A-Z]`</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abCDefGH"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(&)"</span><span style="color: #0000FF;">)</span>
s = gsub(`\ba\b`,s,"another") ?s
<span style="color: #0000FF;">?</span><span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`[A-Z]+`</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"abCDefGH"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(&)"</span><span style="color: #0000FF;">)</span>
?gsub(`string`,s,"replacement")</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`string`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"replacement"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`\ba\b`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"another"</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">gsub</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`string`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"replacement"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,802 ⟶ 2,099:
=={{header|PHP}}==
{{works with|PHP|5.2.0}}
<langsyntaxhighlight lang="php">$string = 'I am a string';
# Test
if (preg_match('/string$/', $string))
Line 1,810 ⟶ 2,107:
# Replace
$string = preg_replace('/\ba\b/', 'another', $string);
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";</langsyntaxhighlight>
 
{{out}}
Line 1,820 ⟶ 2,117:
PicoLisp doesn't have built-in regex functionality.
It is easy to call the native C library.
<langsyntaxhighlight PicoLisplang="picolisp">(let (Pat "a[0-9]z" String "a7z")
(use Preg
(native "@" "regcomp" 'I '(Preg (64 B . 64)) Pat 1) # Compile regex
(when (=0 (native "@" "regexec" 'I (cons NIL (64) Preg) String 0 0 0))
(prinl "String \"" String "\" matches regex \"" Pat "\"") ) ) )</langsyntaxhighlight>
{{out}}
<pre>String "a7z" matches pattern "a[0-9]z"</pre>
Line 1,832 ⟶ 2,129:
Another possibility is dynamic pattern matching,
where arbitrary conditions can be programmed.
<langsyntaxhighlight PicoLisplang="picolisp">(let String "The number <7> is incremented"
(use (@A @N @Z)
(and
(match '(@A "<" @N ">" @Z) (chop String))
(format @N)
(prinl @A "<" (inc @) ">" @Z) ) ) )</langsyntaxhighlight>
{{out}}
<pre>The number <8> is incremented</pre>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">"I am a string" -match '\bstr' # true
"I am a string" -replace 'a\b','no' # I am no string</langsyntaxhighlight>
By default both the <code>-match</code> and <code>-replace</code> operators are case-insensitive. They can be made case-sensitive by using the <code>-cmatch</code> and <code>-creplace</code> operators.
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">String$ = "<tag>some text consisting of Roman letters spaces and numbers like 12</tag>"
regex$ = "<([a-z]*)>[a-z,A-Z,0-9, ]*</\1>"
regex_replace$ = "letters[a-z,A-Z,0-9, ]*numbers[a-z,A-Z,0-9, ]*"
Line 1,855 ⟶ 2,152:
EndIf
Debug ReplaceRegularExpression(2, String$, "char stuff")
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import re
 
string = "This is a string"
Line 1,866 ⟶ 2,163:
 
string = re.sub(" a ", " another ", string)
print(string)</langsyntaxhighlight>
 
=={{header|R}}==
First, define some strings.
<langsyntaxhighlight Rlang="r">pattern <- "string"
text1 <- "this is a matching string"
text2 <- "this does not match"</langsyntaxhighlight>
Matching with grep. The indices of the texts containing matches are returned.
<langsyntaxhighlight Rlang="r">grep(pattern, c(text1, text2)) # 1</langsyntaxhighlight>
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches.
<langsyntaxhighlight Rlang="r">regexpr(pattern, c(text1, text2))</langsyntaxhighlight>
[1] 20 -1
attr(,"match.length")
[1] 6 -1
Replacement
<langsyntaxhighlight Rlang="r">gsub(pattern, "pair of socks", c(text1, text2))</langsyntaxhighlight>
[1] "this is a matching pair of socks" "this does not match"
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,898 ⟶ 2,195:
 
(displayln (regexp-replace " a " s " another "))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>if 'a long string' ~~ /string$/ {
say "It ends with 'string'";
}
 
# substitution has a few nifty features
 
$_ = 'The quick Brown fox';
s:g:samecase/\w+/xxx/;
.say;
# output:
# Xxx xxx Xxx xxx
</syntaxhighlight>
 
=={{header|Raven}}==
 
<langsyntaxhighlight lang="raven">'i am a string' as str</langsyntaxhighlight>
 
Match:
 
<langsyntaxhighlight lang="raven">str m/string$/
if "Ends with 'string'\n" print</langsyntaxhighlight>
 
Replace once:
 
<langsyntaxhighlight lang="raven">str r/ a / another / print</langsyntaxhighlight>
<langsyntaxhighlight lang="raven">str r/ /_/ print</langsyntaxhighlight>
 
Replace all:
 
<langsyntaxhighlight lang="raven">str r/ /_/g print</langsyntaxhighlight>
 
Replace case insensitive:
 
<langsyntaxhighlight lang="raven">str r/ A / another /i print</langsyntaxhighlight>
 
Splitting:
 
<syntaxhighlight lang ="raven">str s/ /</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">REBOL [
Title: "Regular Expression Matching"
URL: http://rosettacode.org/wiki/Regular_expression_matching
Line 1,966 ⟶ 2,278:
 
replace string " another " " a " ; Change string back.
print [crlf "Replacement:" string]</langsyntaxhighlight>
 
{{out}}
Line 1,983 ⟶ 2,295:
 
===testing===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates testing (modeled after Perl example).*/
$string="I am a string"
say 'The string is:' $string
Line 1,990 ⟶ 2,302:
z="ring" ; if pos(z,$string)\==0 then say 'It contains the string:' z
z="ring" ; if wordpos(z,$string)==0 then say 'It does not contain the word:' z
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,001 ⟶ 2,313:
 
===substitution &nbsp; (destructive)===
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates substitution (modeled after Perl example).*/
$string = "I am a string"
old = " a "
Line 2,010 ⟶ 2,322:
$string = changestr(old,$string,new)
say 'The changed string is:' $string
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,021 ⟶ 2,333:
 
===substitution &nbsp; (non-destructive)===
<langsyntaxhighlight lang="rexx">/*REXX program shows non-destructive sub. (modeled after Perl example).*/
$string = "I am a string"
old = " a "
Line 2,031 ⟶ 2,343:
say 'The original string is:' $string
say 'The changed string is:' $string2
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,042 ⟶ 2,354:
 
===test and substitute===
<langsyntaxhighlight lang="rexx">/*REXX program shows test and substitute (modeled after Perl example).*/
$string = "I am a string"
old = " am "
Line 2,055 ⟶ 2,367:
say 'I was able to find and replace ' old " with " new
end
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,067 ⟶ 2,379:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Regular expressions
 
Line 2,077 ⟶ 2,389:
text = left(text,i - 1) + "was" + substr(text,i + 2)
see "replace 'am' with 'was' = " + text + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,086 ⟶ 2,398:
=={{header|Ruby}}==
Test
<langsyntaxhighlight lang="ruby">str = "I am a string"
p "Ends with 'string'" if str =~ /string$/
p "Does not start with 'You'" unless str =~ /^You/</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="ruby">str.sub(/ a /, ' another ') #=> "I am another string"
# Or:
str[/ a /] = ' another ' #=> "another"
str #=> "I am another string"</langsyntaxhighlight>
 
Substitute using block
<langsyntaxhighlight lang="ruby">str.gsub(/\bam\b/) { |match| match.upcase } #=> "I AM a string"</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">string$ = "I am a string"
if right$(string$,6) = "string" then print "'";string$;"' ends with 'string'"
i = instr(string$,"am")
string$ = left$(string$,i - 1) + "was" + mid$(string$,i + 2)
print "replace 'am' with 'was' = ";string$
</syntaxhighlight>
</lang>
{{out}}
<pre>'I am a string' ends with 'string'
Line 2,112 ⟶ 2,424:
=={{header|Rust}}==
Note that <code>Regex::new</code> checks for a valid regex and thus returns a <code>Result<Regex, Error></code>.
<langsyntaxhighlight Rustlang="rust">use regex::Regex;
 
fn main() {
Line 2,122 ⟶ 2,434:
 
println!("{}", Regex::new(" a ").unwrap().replace(s, " another "));
}</langsyntaxhighlight>
 
=={{header|Sather}}==
Sather understands POSIX regular expressions.
 
<langsyntaxhighlight lang="sather">class MAIN is
-- we need to implement the substitution
regex_subst(re:REGEXP, s, sb:STR):STR is
Line 2,148 ⟶ 2,460:
#OUT + regex_subst(REGEXP::regexp("am +a +st", true), s, "get the ") + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
Define
<langsyntaxhighlight Scalalang="scala">val Bottles1 = "(\\d+) bottles of beer".r // syntactic sugar
val Bottles2 = """(\d+) bottles of beer""".r // using triple-quotes to preserve backslashes
val Bottles3 = new scala.util.matching.Regex("(\\d+) bottles of beer") // standard
val Bottles4 = new scala.util.matching.Regex("""(\d+) bottles of beer""", "bottles") // with named groups</langsyntaxhighlight>
 
Search and replace with string methods:
<langsyntaxhighlight lang="scala">"99 bottles of beer" matches "(\\d+) bottles of beer" // the full string must match
"99 bottles of beer" replace ("99", "98") // Single replacement
"99 bottles of beer" replaceAll ("b", "B") // Multiple replacement</langsyntaxhighlight>
 
Search with regex methods:
<langsyntaxhighlight lang="scala">"\\d+".r findFirstIn "99 bottles of beer" // returns first partial match, or None
"\\w+".r findAllIn "99 bottles of beer" // returns all partial matches as an iterator
"\\s+".r findPrefixOf "99 bottles of beer" // returns a matching prefix, or None
Bottles4 findFirstMatchIn "99 bottles of beer" // returns a "Match" object, or None
Bottles4 findPrefixMatchOf "99 bottles of beer" // same thing, for prefixes
val bottles = (Bottles4 findFirstMatchIn "99 bottles of beer").get.group("bottles") // Getting a group by name</langsyntaxhighlight>
 
Using pattern matching with regex:
<langsyntaxhighlight Scalalang="scala">val Some(bottles) = Bottles4 findPrefixOf "99 bottles of beer" // throws an exception if the matching fails; full string must match
for {
line <- """|99 bottles of beer on the wall
Line 2,184 ⟶ 2,496:
for {
matched <- "(\\w+)".r findAllIn "99 bottles of beer" matchData // matchData converts to an Iterator of Match
} println("Matched from "+matched.start+" to "+matched.end)</langsyntaxhighlight>
 
Replacing with regex:
<langsyntaxhighlight Scalalang="scala">Bottles2 replaceFirstIn ("99 bottles of beer", "98 bottles of beer")
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
 
Basic example showing the use of SenseTalk's pattern language to create a pattern, test for a match, find all matches, and replace a match.
<syntaxhighlight lang="sensetalk">
set text to "This is a story about R2D2 and C3P0 who are best friends."
set pattern to <word start, letter, digit, letter, digit, word end>
 
put the sixth word of text matches pattern -- (note: the sixth word is "R2D2")
 
put every occurrence of pattern in text
 
replace the second occurrence of pattern in text with "Luke"
put text
</syntaxhighlight>
Output
<syntaxhighlight lang="sensetalk">
True
(R2D2,C3P0)
This is a story about R2D2 and Luke who are best friends.
</syntaxhighlight>
 
Advanced example showing how to use capture groups within a pattern to reformat the names in a list.
<syntaxhighlight lang="sensetalk">
set phoneList to {{
Harry Potter 98951212
Hermione Granger 59867125
Ron Weasley 56471832
 
}}
 
set wordPattern to <word start, characters, word end>
set namePattern to <start of line, {firstName: wordPattern}, space, {lastName: wordPattern}>
 
replace every occurrence of namePattern in phoneList with "{:lastName}, {:firstName} –"
put phoneList
</syntaxhighlight>
Output
<syntaxhighlight lang="sensetalk">
Potter, Harry – 98951212
Granger, Hermione – 59867125
Weasley, Ron – 56471832
</syntaxhighlight>
 
=={{header|Shiny}}==
<langsyntaxhighlight lang="shiny">str: 'I am a string'</langsyntaxhighlight>
 
Match text:
<langsyntaxhighlight lang="shiny">if str.match ~string$~
say "Ends with 'string'"
end</langsyntaxhighlight>
 
Replace text:
<langsyntaxhighlight lang="shiny">say str.alter ~ a ~ 'another'</langsyntaxhighlight>
 
=={{header|Sidef}}==
Simple matching:
<langsyntaxhighlight lang="ruby">var str = "I am a string";
if (str =~ /string$/) {
print "Ends with 'string'\n";
}</langsyntaxhighlight>
 
Global matching:
<langsyntaxhighlight lang="ruby">var str = <<'EOF';
x:Foo
y:Bar
Line 2,215 ⟶ 2,571:
while (var m = str=~/(\w+):(\S+)/g) {
say "#{m[0]} -> #{m[1]}";
}</langsyntaxhighlight>
 
Substitutions:
<langsyntaxhighlight lang="ruby">var str = "I am a string";
 
# Substitute something mached by a regex
Line 2,229 ⟶ 2,585:
str = str.gsub(/(\w+)/, {|s1| 'x' * s1.len}); # globaly replace any word with 'xxx'
 
say str; # prints: 'x xx xxxxxx'</langsyntaxhighlight>
 
=={{header|Slate}}==
Line 2,235 ⟶ 2,591:
This library is still in its early stages. There isn't currently a feature to replace a substring.
 
<langsyntaxhighlight lang="slate">
'http://slatelanguage.org/test/page?query' =~ '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?'.
 
" ==> {'http:'. 'http'. '//slatelanguage.org'. 'slatelanguage.org'. '/test/page'. '?query'. 'query'. Nil} "
</syntaxhighlight>
</lang>
 
=={{header|Smalltalk}}==
 
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">|re s s1|
re := Regex fromString: '[a-z]+ing'.
s := 'this is a matching string'.
Line 2,261 ⟶ 2,617:
].
 
(s replacingRegex: re with: 'modified') displayNl.</langsyntaxhighlight>
 
{{works with|Pharo}}
<langsyntaxhighlight lang="smalltalk">
|re s s1|
re := 'm[a-z]+ing' asRegex.
Line 2,272 ⟶ 2,628:
s1 := re copy: s replacingMatchesWith: 'modified'.
</syntaxhighlight>
</lang>
 
=={{header|SNOBOL4}}==
Line 2,282 ⟶ 2,638:
SNOBOL4's "raison d'etre" is pattern matching and string manipulation (although it's also strong in data structures too). The basic statement syntax in SNOBOL4 is:
 
<langsyntaxhighlight lang="snobol4">label subject pattern = object :(goto)</langsyntaxhighlight>
 
The basic operation is to evaluate the subject, evaluate the pattern, find the pattern in the subject, evaluate the object, and then replace the portion of the subject matched by the pattern with the evaluated object. If any of those steps fails (i.e. does not succeed) then execution continues with the goto, as appropriate.
Line 2,288 ⟶ 2,644:
The goto can be unconditional, or can be based on whether the statement succeeded or failed (and that is the basis for all explicit transfers of control in SNOBOL4). This example finds the string "SNOBOL4" in string variable string1, and replaces it with "new SPITBOL" (SPITBOL is an implementation of SNOBOL4, basically SPITBOL is to SNOBOL4 what Turbo Pascal is to Pascal):
 
<langsyntaxhighlight lang="snobol4"> string1 = "The SNOBOL4 language is designed for string manipulation."
string1 "SNOBOL4" = "new SPITBOL" :s(changed)f(nochange)</langsyntaxhighlight>
 
The following example replaces "diameter is " and a numeric value by "circumference is " and the circumference instead (it also shows creation of a pattern which matches integer or real numeric values, and storing that pattern into a variable... and then using that pattern variable later in a slightly more complicated pattern expression):
 
<langsyntaxhighlight lang="snobol4"> pi = 3.1415926
dd = "0123456789"
string1 = "For the first circle, the diameter is 2.5 inches."
numpat = span(dd) (("." span(dd)) | null)
string1 "diameter is " numpat . diam = "circumference is " diam * pi</langsyntaxhighlight>
 
Relatively trivial pattern matching and replacements can be attacked very effectively using regular expressions, but regular expressions (while ubiquitous) are a crippling limitation for more complicated pattern matching problems.
Line 2,305 ⟶ 2,661:
{{works with|SML/NJ}}
Test
<langsyntaxhighlight lang="sml">CM.make "$/regexp-lib.cm";
structure RE = RegExpFn (
structure P = AwkSyntax
Line 2,318 ⟶ 2,674:
in
print ("matched at position " ^ Int.toString pos ^ "\n")
end;</langsyntaxhighlight>
 
=={{header|Stata}}==
See '''[http://www.stata.com/help.cgi?regexm regexm]''', '''regexr''' and '''regexs''' in Stata help.
 
<langsyntaxhighlight lang="stata">scalar s="ars longa vita brevis"
 
* is there a vowel?
Line 2,329 ⟶ 2,685:
 
* replace the first vowel with "?"
di regexr(s,"[aeiou]","?")</langsyntaxhighlight>
 
=={{header|Swift}}==
 
===RegularExpressionSearch===
Test
<langsyntaxhighlight lang="swift">import Foundation
 
let str = "I am a string"
if let range = str.rangeOfString("string$", options: .RegularExpressionSearch) {
println("Ends with 'string'")
}</langsyntaxhighlight>
 
Substitute (undocumented)
<langsyntaxhighlight lang="swift">import Foundation
 
let orig = "I am the original string"
let result = orig.stringByReplacingOccurrencesOfString("original", withString: "modified", options: .RegularExpressionSearch)
println(result)</langsyntaxhighlight>
 
===NSRegularExpression===
Test
<langsyntaxhighlight lang="swift">import Foundation
 
if let regex = NSRegularExpression(pattern: "string$", options: nil, error: nil) {
Line 2,357 ⟶ 2,714:
println("Ends with 'string'")
}
}</langsyntaxhighlight>
 
Loop through matches
<langsyntaxhighlight lang="swift"> for x in regex.matchesInString(str, options: nil, range: NSRange(location: 0, length: count(str.utf16))) {
let match = x as! NSTextCheckingResult
// match.range gives the range of the whole match
// match.rangeAtIndex(i) gives the range of the i'th capture group (starting from 1)
}</langsyntaxhighlight>
 
Substitute
<langsyntaxhighlight lang="swift">import Foundation
 
let orig = "I am the original string"
Line 2,373 ⟶ 2,730:
let result = regex.stringByReplacingMatchesInString(orig, options: nil, range: NSRange(location: 0, length: count(orig.utf16)), withTemplate: "modified")
println(result)
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Test using <code>regexp</code>:
<langsyntaxhighlight lang="tcl">set theString "I am a string"
if {[regexp -- {string$} $theString]} {
puts "Ends with 'string'"
Line 2,384 ⟶ 2,741:
if {![regexp -- {^You} $theString]} {
puts "Does not start with 'You'"
}</langsyntaxhighlight>
 
Extract substring using <code>regexp</code>
<langsyntaxhighlight lang="tcl">set theString "This string has >123< a number in it"
if {[regexp -- {>(\d+)<} $theString -> number]} {
puts "Contains the number $number"
}</langsyntaxhighlight>
 
Substitute using <code>regsub</code>
<langsyntaxhighlight lang="tcl">set theString = "I am a string"
puts [regsub -- { +a +} $theString { another }]</langsyntaxhighlight>
 
=={{header|Toka}}==
Line 2,400 ⟶ 2,757:
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.
 
<langsyntaxhighlight lang="toka">#! Include the regex library
needs regex
 
Line 2,418 ⟶ 2,775:
#! try-regex will return a flag. -1 is TRUE, 0 is FALSE
expression test.1 2 match try-regex .
expression test.2 2 match try-regex .</langsyntaxhighlight>
 
=={{header|TXR}}==
Line 2,426 ⟶ 2,783:
Txr is not designed for sed-like filtering, but here is how to do <code>sed -e 's/dog/cat/g'</code>:
 
<langsyntaxhighlight lang="txr">@(collect)
@(coll :gap 0)@mismatch@{match /dog/}@(end)@suffix
@(output)
@(rep)@{mismatch}cat@(end)@suffix
@(end)
@(end)</langsyntaxhighlight>
 
How it works is that the body of the <code>coll</code> uses a double-variable match:
Line 2,451 ⟶ 2,808:
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.
 
<langsyntaxhighlight lang="txr">@(freeform)
@(coll :gap 0)@notcomment@{comment /[/][*].%[*][/]/}@(end)@tail
@(output)
@(rep)@notcomment @(end)@tail
@(end)</langsyntaxhighlight>
 
===Regexes in TXR Lisp===
Line 2,461 ⟶ 2,818:
Parse regex at run time to abstract syntax:
 
<langsyntaxhighlight lang="sh">$ txr -p '(regex-parse "a.*b")'
(compound #\a (0+ wild) #\b)</langsyntaxhighlight>
 
Dynamically compile regex abstract syntax to regex object:
 
<langsyntaxhighlight lang="sh">$ txr -p "(regex-compile '(compound #\a (0+ wild) #\b))"
#<sys:regex: 9c746d0></langsyntaxhighlight>
 
Search replace with <code>regsub</code>.
 
<langsyntaxhighlight lang="sh">$ txr -p '(regsub #/a+/ "-" "baaaaaad")'
"b-d"</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 2,484 ⟶ 2,841:
{{works with|bash}}
{{works with|ksh}}
<langsyntaxhighlight lang="bash">s="I am a string"
if [[ $s =~ str..g$ ]]; then
echo "the string ends with 'str..g'"
fi</langsyntaxhighlight>
 
===Replacing===
Given these values
<langsyntaxhighlight lang="bash">s="I am the original string"
re='o.*l'
repl="modified"</langsyntaxhighlight>
 
{{works with|ksh}}
Can use regular expressions in parameter expansion
<langsyntaxhighlight lang="bash">modified=${s/~(E)$re/$repl}
echo "$modified" # I am the modified string</langsyntaxhighlight>
 
{{works with|bash}}
have to break apart the original string to build the modified string.
<langsyntaxhighlight lang="bash">if [[ $s =~ $re ]]; then
submatch=${BASH_REMATCH[0]}
modified="${s%%$submatch*}$repl${s#*$submatch}"
echo "$modified" # I am the modified string
fi</langsyntaxhighlight>
 
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">
void main(){
string sentence = "This is a sample sentence.";
Line 2,524 ⟶ 2,880:
stdout.printf("Replaced sentence is: %s\n", sentence);
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,534 ⟶ 2,890:
=={{header|VBScript}}==
Replace white spaces with line breaks.
<langsyntaxhighlight lang="vb">text = "I need more coffee!!!"
Set regex = New RegExp
regex.Global = True
Line 2,542 ⟶ 2,898:
Else
WScript.StdOut.Write "No matching pattern"
End If</langsyntaxhighlight>
{{in}}
<pre>I need more coffee!!!</pre>
Line 2,557 ⟶ 2,913:
 
Match text at cursor location:
<langsyntaxhighlight lang="vedit">if (Match(".* string$", REGEXP)==0) {
Statline_Message("This line ends with 'string'")
}</langsyntaxhighlight>
 
Search for a pattern:
<langsyntaxhighlight lang="vedit">if (Search("string$", REGEXP+NOERR)) {
Statline_Message("'string' at and of line found")
}</langsyntaxhighlight>
 
Replace:
<langsyntaxhighlight lang="vedit">Replace(" a ", " another ", REGEXP+NOERR)</langsyntaxhighlight>
 
=={{header|Web 68}}==
<langsyntaxhighlight lang="web68">@1Introduction.
Web 68 has access to a regular expression module
which can compile regular expressions,
Line 2,627 ⟶ 2,983:
ca -l mod rosettacoderegex.w68
That's it. The resulting binary will print
'String "string$" matches string "This is a string"'</langsyntaxhighlight>
 
=={{header|Wren}}==
===Version 1 (Wren-pattern)===
{{libheader|Wren-pattern}}
The above module allows us to do simple string pattern matching and replacement in a similar fashion to regular expressions but using a different syntax. SenseTalk's examples have been borrowed for this purpose.
<syntaxhighlight lang="wren">import "./pattern" for Pattern
 
var s = "This is a story about R2D2 and C3P0 who are best friends."
var p = Pattern.new("/u/d/u/d")
var matches = p.findAll(s)
System.print("Original string:\n%(" %(s)")")
 
System.print("\nThe following matches were found:")
matches.each{ |m| System.print(" %(m.text) at index %(m.index)") }
 
System.print("\nAfter replacing the second match:")
System.print(" %(p.replace(s, "Luke", 2, 1))") // replace 2nd match with "Luke"
 
System.print("\nReformatted phone list example:")
var phoneList = [
"Harry Potter 98951212",
"Hermione Granger 59867125",
"Ron Weasley 56471832"
]
var p2 = Pattern.new("[+1/a] [+1/a] [=8/d]")
for (record in phoneList) {
var m = p2.find(record)
var t = m.capsText
System.print(" %(t[1]), %(t[0]) - %(t[2])")
}</syntaxhighlight>
 
{{out}}
<pre>
Original string:
This is a story about R2D2 and C3P0 who are best friends.
 
The following matches were found:
R2D2 at index 22
C3P0 at index 31
 
After replacing the second match:
This is a story about R2D2 and Luke who are best friends.
 
Reformatted phone list example:
Potter, Harry - 98951212
Granger, Hermione - 59867125
Weasley, Ron - 56471832
</pre>
<br>
===Version 2 (Wren-regex)===
{{libheader|Wren-regex}}
The above module uses RE2 regular expression syntax but, since it is a wrapper for Go's 'regexp' package, it can only be used from a special embedded application and not by Wren-cli.
<syntaxhighlight lang="wren">/* Regular_expressions_2.wren */
import "./regex" for Regex
 
var s = "This is a story about R2D2 and C3P0 who are best friends."
var re = Regex.compile("""[A-Z]\d[A-Z]\d""")
var matches = re.findAll(s)
var indices = re.findAllIndex(s)
System.print("Original string:\n%(" %(s)")")
 
System.print("\nThe following matches were found:")
for (i in 0...matches.count) {
var m = matches[i]
var ix = indices[i][0]
System.print(" %(m) at index %(ix)")
}
 
System.print("\nAfter replacing the second match:")
System.print(" %(re.replaceAll(s, "Luke", 2, 1))") // replace 2nd match with "Luke"
 
System.print("\nReformatted phone list example:")
var phoneList = [
"Harry Potter 98951212",
"Hermione Granger 59867125",
"Ron Weasley 56471832"
]
 
var re2 = Regex.compile("""([A-Za-z]+) ([A-Za-z]+) (\d{8})""")
for (record in phoneList) {
var m = re2.findSubmatch(record)
System.print(" %(m[2]), %(m[1]) - %(m[3])")
}</syntaxhighlight>
 
{{out}}
<pre>
Identical to first version.
</pre>
 
=={{header|zkl}}==
The build in RE engine is a basic one.<br/>
Strings are immutable so replacement is creation.
<langsyntaxhighlight lang="zkl">var re=RegExp(".*string$");
re.matches("I am a string") //-->True
var s="I am a string thing"
Line 2,642 ⟶ 3,086:
re.search(s,True); // using .matched clears it
m:=re.matched[1];
s.replace(m,"FOO"); // -->"I am a FOO thing"</langsyntaxhighlight>
 
Using a mutable byte bucket:
<langsyntaxhighlight lang="zkl">var s=Data(0,Int,"I am a string thing");
re.search(s,True);
p,n:=re.matched[0];
s[p,n]="FOO";
s.text //-->"I am a FOO thing"</langsyntaxhighlight>
885

edits