Regular expressions: Difference between revisions
(→{{header|REXX}}: add REXX examples modeled after PERL examples. -- ~~~~) |
(PascalABC.NET) |
||
(124 intermediate revisions by 62 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Text processing}} |
{{task|Text processing}} |
||
[[Category:Regular expressions]] |
|||
{{omit from|BASIC}} |
|||
{{omit from|Brlcad}} |
|||
{{omit from|EasyLang}} |
|||
{{omit from|GUISS}} |
|||
{{omit from|PARI/GP}} |
|||
The goal of this task is |
|||
;Task: |
|||
* to match a string against a regular expression |
|||
* |
:* match a string against a regular expression |
||
:* 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}}== |
|||
<syntaxhighlight lang="forth"> |
|||
"haystack" /a./ r:match . cr |
|||
"haystack" /a./ "blah" s:replace! . cr |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 |
|||
hblahstblahk |
|||
</pre> |
|||
=={{header|ABAP}}== |
|||
<syntaxhighlight lang="abap"> |
|||
DATA: text TYPE string VALUE 'This is a Test'. |
|||
FIND FIRST OCCURRENCE OF REGEX 'is' IN text. |
|||
IF sy-subrc = 0. |
|||
cl_demo_output=>write( 'Regex matched' ). |
|||
ENDIF. |
|||
REPLACE ALL OCCURRENCES OF REGEX '[t|T]est' IN text WITH 'Regex'. |
|||
cl_demo_output=>write( text ). |
|||
cl_demo_output=>display( ). |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
Regex matched |
|||
This is a Regex |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
There is no Regular Expression library in the Ada Standard, |
There is no Regular Expression library in the Ada Standard, |
||
so I am using one of the libraries provided by gnat/gcc. |
|||
<lang ada>with Ada.Text_IO; with Gnat.Regpat; use Ada.Text_IO; |
|||
<syntaxhighlight lang="ada">with Ada.Text_IO; with Gnat.Regpat; use Ada.Text_IO; |
|||
procedure Regex is |
procedure Regex is |
||
Line 49: | Line 109: | ||
Str := Str(Str'First .. First-1) & "pattern" & Str(Last+1 .. Str'Last); |
Str := Str(Str'First .. First-1) & "pattern" & Str(Last+1 .. Str'Last); |
||
Put_Line(Str); |
Put_Line(Str); |
||
end Regex;</ |
end Regex;</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre><I> |
<pre><I> |
||
<love> |
<love> |
||
Line 58: | Line 117: | ||
<matching> |
<matching> |
||
I love pattern matching!</pre> |
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> |
|||
Output: |
|||
<pre> |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
The routines ''grep in strings'' and ''sub in string'' |
The routines ''grep in strings'' and ''sub in string'' |
||
are not part of [[ALGOL 68]]'s standard prelude. |
|||
<!-- {{does not work with|ALGOL 68|Standard - grep/sub in string are not part of the standard's prelude. }} --> |
<!-- {{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}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">INT match=0, no match=1, out of memory error=2, other error=3; |
||
STRING str := "i am a string"; |
STRING str := "i am a string"; |
||
Line 96: | Line 136: | ||
# Replace: # |
# Replace: # |
||
IF sub in string(" a ", " another ",str) = match THEN printf(($gl$, str)) FI;</ |
IF sub in string(" a ", " another ",str) = match THEN printf(($gl$, str)) FI;</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Ends with "string" |
Ends with "string" |
||
Line 103: | Line 143: | ||
</pre> |
</pre> |
||
Standard ALGOL 68 does have an primordial form of pattern matching |
Standard ALGOL 68 does have an primordial form of pattern matching |
||
called a '''format'''. |
called a '''format'''. |
||
This is designed to extract values from input data. |
|||
But it can also be used for outputting (and transputting) the original data. |
But it can also be used for outputting (and transputting) the original data. |
||
Line 111: | Line 152: | ||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
||
For example:< |
For example:<syntaxhighlight lang="algol68">FORMAT pattern = $ddd" "c("cats","dogs")$; |
||
FILE file; STRING book; associate(file, book); |
FILE file; STRING book; associate(file, book); |
||
on value error(file, (REF FILE f)BOOL: stop); |
on value error(file, (REF FILE f)BOOL: stop); |
||
Line 122: | Line 163: | ||
print(("Dalmatians: ", dalmatians, new line)); |
print(("Dalmatians: ", dalmatians, new line)); |
||
count OF dalmatians +:=1; |
count OF dalmatians +:=1; |
||
printf(($"Gives: "$, pattern, dalmatians, $l$))</ |
printf(($"Gives: "$, pattern, dalmatians, $l$))</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Dalmatians: +100 +2 |
Dalmatians: +100 +2 |
||
Gives 101 dogs |
Gives 101 dogs |
||
</pre> |
</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}}== |
=={{header|Argile}}== |
||
< |
<syntaxhighlight lang="argile">use std, regex |
||
(: matching :) |
(: matching :) |
||
Line 151: | Line 338: | ||
uninit regex |
uninit regex |
||
check mem leak; use dbg (:optional:)</ |
check mem leak; use dbg (:optional:)</syntaxhighlight> |
||
(note that it needs to be compiled with argrt library) |
(note that it needs to be compiled with argrt library) |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
string matches |
string matches |
||
$0m3 4ll0c4t3d $tr1ng |
$0m3 4ll0c4t3d $tr1ng |
||
</pre> |
</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}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % foundpos := RegExMatch("Hello World", "World$") |
||
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")</ |
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
AWK supports regular expressions, which are typically |
AWK supports regular expressions, which are typically enclosed using slash symbols at the front and back, and the tilde regular expression binding operator: |
||
< |
<syntaxhighlight lang="awk">$ awk '{if($0~/[A-Z]/)print "uppercase detected"}' |
||
abc |
abc |
||
ABC |
ABC |
||
uppercase detected</ |
uppercase detected</syntaxhighlight> |
||
As shorthand, a regular expression in the condition part fires if it matches an input line: |
As shorthand, a regular expression in the condition part fires if it matches an input line: |
||
< |
<syntaxhighlight lang="awk">awk '/[A-Z]/{print "uppercase detected"}' |
||
def |
def |
||
DeF |
DeF |
||
uppercase detected</ |
uppercase detected</syntaxhighlight> |
||
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): |
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): |
||
< |
<syntaxhighlight lang="awk">$ awk '{gsub(/[A-Z]/,"*");print}' |
||
abCDefG |
abCDefG |
||
ab**ef* |
ab**ef* |
||
$ awk '{gsub(/[A-Z]/,"(&)");print}' |
$ awk '{gsub(/[A-Z]/,"(&)");print}' |
||
abCDefGH |
abCDefGH |
||
ab(C)(D)ef(G)(H)</ |
ab(C)(D)ef(G)(H)</syntaxhighlight> |
||
This variant matches one or more uppercase letters in one round: |
This variant matches one or more uppercase letters in one round: |
||
< |
<syntaxhighlight lang="awk">$ awk '{gsub(/[A-Z]+/,"(&)");print}' |
||
abCDefGH |
abCDefGH |
||
ab(CD)ef(GH)</ |
ab(CD)ef(GH)</syntaxhighlight> |
||
Regular expression negation can be achieved by combining the regular expression binding operator with a logical not operator, as follows: |
|||
if (text !~ /strawberry/) { |
|||
print "Match not found" |
|||
} |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
Uses the [http://people.delphiforums.com/gjc/gnu_regex.html gnu_regex] library. |
Uses the [http://people.delphiforums.com/gjc/gnu_regex.html gnu_regex] library. |
||
< |
<syntaxhighlight lang="bbcbasic"> SYS "LoadLibrary", "gnu_regex.dll" TO gnu_regex% |
||
IF gnu_regex% = 0 ERROR 100, "Cannot load gnu_regex.dll" |
IF gnu_regex% = 0 ERROR 100, "Cannot load gnu_regex.dll" |
||
SYS "GetProcAddress", gnu_regex%, "regcomp" TO regcomp |
SYS "GetProcAddress", gnu_regex%, "regcomp" TO regcomp |
||
Line 230: | Line 437: | ||
ENDIF |
ENDIF |
||
SYS "FreeLibrary", gnu_regex%</ |
SYS "FreeLibrary", gnu_regex%</syntaxhighlight> |
||
{{out}} |
|||
'''Output:''' |
|||
<pre> |
<pre> |
||
<I> |
<I> |
||
Line 241: | Line 448: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Pattern matching in Bracmat is inspired by pattern matching in Snobol. |
|||
Pattern matching in Bracmat is inspired by pattern matching in Snobol. It also is quite different from regular expressions:<ul><li>Patterns in Bracmat are not greedy</li><li>It is not possible to replace substrings, because values can never be changed</li><li>Patterns always must match all of the subject</li><li>Strings as well as complex data can be subjected to pattern matching</li></ul> |
|||
It also is quite different from regular expressions: |
|||
* Patterns in Bracmat are not greedy |
|||
* It is not possible to replace substrings, because values can never be changed |
|||
* Patterns always must match all of the subject |
|||
* Strings as well as complex data can be subjected to pattern matching |
|||
List all rational numbers smaller then 7 hidden in the string "fgsakg789/35768685432fkgha" |
List all rational numbers smaller then 7 hidden in the string "fgsakg789/35768685432fkgha" |
||
< |
<syntaxhighlight lang="bracmat">@("fesylk789/35768poq2art":? (#<7:?n & out$!n & ~) ?)</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>789/357 |
<pre>789/357 |
||
789/3576 |
789/3576 |
||
Line 264: | Line 476: | ||
After the last number, the match expression fails. |
After the last number, the match expression fails. |
||
=={{header|Brat}}== |
|||
Test |
|||
<syntaxhighlight lang="brat">str = "I am a string" |
|||
true? str.match(/string$/) |
|||
{ p "Ends with 'string'" } |
|||
false? str.match(/^You/) |
|||
{ p "Does not start with 'You'" } |
|||
</syntaxhighlight> |
|||
Substitute |
|||
<syntaxhighlight lang="brat"># Substitute in copy |
|||
str2 = str.sub(/ a /, " another ") |
|||
p str # original unchanged |
|||
p str2 # prints "I am another string" |
|||
# Substitute in place |
|||
str.sub!(/ a /, " another ") |
|||
p str # prints "I am another string" |
|||
# Substitute with a block |
|||
str.sub! /a/ |
|||
{ match | match.upcase } |
|||
p str # prints "I Am Another string" |
|||
</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 270: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <sys/types.h> |
#include <sys/types.h> |
||
Line 312: | Line 560: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===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++}}== |
=={{header|C++}}== |
||
{{works with|g++|4.0.2}} |
{{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 <string> |
||
#include <iterator> |
#include <iterator> |
||
#include < |
#include <regex> |
||
int main() |
int main() |
||
{ |
{ |
||
std::regex re(".* string$"); |
|||
std::string s = "Hi, I am a string"; |
std::string s = "Hi, I am a string"; |
||
// match the complete string |
// match the complete string |
||
if ( |
if (std::regex_match(s, re)) |
||
std::cout << "The string matches.\n"; |
std::cout << "The string matches.\n"; |
||
else |
else |
||
Line 335: | Line 666: | ||
// match a substring |
// match a substring |
||
std::regex re2(" a.*a"); |
|||
std::smatch match; |
|||
if ( |
if (std::regex_search(s, match, re2)) |
||
{ |
{ |
||
std::cout << "Matched " << match.length() |
std::cout << "Matched " << match.length() |
||
Line 351: | Line 682: | ||
// replace a substring |
// replace a substring |
||
std::string dest_string; |
std::string dest_string; |
||
std::regex_replace(std::back_inserter(dest_string), |
|||
s.begin(), s.end(), |
s.begin(), s.end(), |
||
re2, |
re2, |
||
"'m now a changed"); |
"'m now a changed"); |
||
std::cout << dest_string << std::endl; |
std::cout << dest_string << std::endl; |
||
}</ |
}</syntaxhighlight> |
||
=={{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}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(let [s "I am a string"] |
||
;; match |
;; match |
||
(when (re-find #"string$" s) |
(when (re-find #"string$" s) |
||
Line 385: | Line 699: | ||
;; substitute |
;; substitute |
||
(println (clojure.string/replace s " a " " another ")) |
(println (clojure.string/replace s " a " " another ")) |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 392: | Line 706: | ||
Uses [http://weitz.de/cl-ppcre/ CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp]. |
Uses [http://weitz.de/cl-ppcre/ CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp]. |
||
< |
<syntaxhighlight lang="lisp">(let ((string "I am a string")) |
||
(when (cl-ppcre:scan "string$" string) |
(when (cl-ppcre:scan "string$" string) |
||
(write-line "Ends with string")) |
(write-line "Ends with string")) |
||
(unless (cl-ppcre:scan "^You" string ) |
(unless (cl-ppcre:scan "^You" string ) |
||
(write-line "Does not start with 'You'")))</ |
(write-line "Does not start with 'You'")))</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="lisp">(let* ((string "I am a string") |
||
(string (cl-ppcre:regex-replace " a " string " another "))) |
(string (cl-ppcre:regex-replace " a " string " another "))) |
||
(write-line string))</ |
(write-line string))</syntaxhighlight> |
||
Test and Substitute |
Test and Substitute |
||
< |
<syntaxhighlight lang="lisp">(let ((string "I am a string")) |
||
(multiple-value-bind (string matchp) |
(multiple-value-bind (string matchp) |
||
(cl-ppcre:regex-replace "\\bam\\b" string "was") |
(cl-ppcre:regex-replace "\\bam\\b" string "was") |
||
(when matchp |
(when matchp |
||
(write-line "I was able to find and replace 'am' with 'was'."))))</ |
(write-line "I was able to find and replace 'am' with 'was'."))))</syntaxhighlight> |
||
===CLISP regexp engine=== |
===CLISP regexp engine=== |
||
{{works with|CLISP}} |
{{works with|CLISP}} |
||
[[Clisp]] comes with [http://clisp.org/impnotes/regexp-mod.html built-in regexp matcher]. On a Clisp prompt: |
[[Clisp]] comes with [http://clisp.org/impnotes/regexp-mod.html built-in regexp matcher]. On a Clisp prompt: |
||
< |
<syntaxhighlight lang="lisp">[1]> (regexp:match "fox" "quick fox jumps") |
||
#S(REGEXP:MATCH :START 6 :END 9)</ |
#S(REGEXP:MATCH :START 6 :END 9)</syntaxhighlight> |
||
To find all matches, loop with different <code>:start</code> keyword. |
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: |
Replacing text can be done with the help of <code>REGEXP:REGEXP-SPLIT</code> function: |
||
< |
<syntaxhighlight lang="lisp">[2]> (defun regexp-replace (pat repl string) |
||
(reduce #'(lambda (x y) (string-concat x repl y)) |
(reduce #'(lambda (x y) (string-concat x repl y)) |
||
(regexp:regexp-split pat string))) |
(regexp:regexp-split pat string))) |
||
REGEXP-REPLACE |
REGEXP-REPLACE |
||
[3]> (regexp-replace "x\\b" "-X-" "quick foxx jumps") |
[3]> (regexp-replace "x\\b" "-X-" "quick foxx jumps") |
||
"quick fox-X- jumps"</ |
"quick fox-X- jumps"</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang="d">void main() { |
|||
<lang d>import std.stdio, std.regex; |
|||
import std.stdio, std.regex; |
|||
immutable s = "I am a string"; |
|||
void main() { |
|||
immutable string s = "I am a string"; |
|||
// Test |
// Test. |
||
if ( |
if (s.match("string$")) |
||
"Ends with 'string'.".writeln; |
|||
// Substitute |
// Substitute. |
||
replace |
s.replace(" a ".regex, " another ").writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Ends with 'string'. |
<pre>Ends with 'string'. |
||
I am another string</pre> |
I am another string</pre> |
||
In std.string there are string functions to perform the same operations more efficiently. |
In std.string there are string functions to perform the same operations more efficiently. |
||
=={{header|Dart}}== |
|||
<syntaxhighlight lang="d">RegExp regexp = new RegExp(r'\w+\!'); |
|||
String capitalize(Match m) => '${m[0].substring(0, m[0].length-1).toUpperCase()}'; |
|||
void main(){ |
|||
String hello = 'hello hello! world world!'; |
|||
String hellomodified = hello.replaceAllMapped(regexp, capitalize); |
|||
print(hello); |
|||
print(hellomodified); |
|||
}</syntaxhighlight> |
|||
{{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"> |
|||
str = "This is a string" |
|||
if str =~ ~r/string$/, do: IO.inspect "str ends with 'string'" |
|||
</syntaxhighlight> |
|||
A number of modifiers can be appended to the regular expression; <code>~r/pattern/i</code>, for instance, toggles case insensitivity. |
|||
<syntaxhighlight lang="elixir"> |
|||
str =~ ~r/this/ # => false |
|||
str =~ ~r/this/i # => true |
|||
</syntaxhighlight> |
|||
Both <code>Regex</code> and <code>String</code> have a <code>replace</code> function. |
|||
<syntaxhighlight lang="elixir"> |
|||
str1 = ~r/a/ |> Regex.replace(str,"another") |
|||
str2 = str1 |> String.replace(~r/another/,"even another") |
|||
</syntaxhighlight> |
|||
<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"> |
|||
str3 = ~r/another/ |> Regex.replace(str2, fn x -> "#{String.upcase(x)}" end) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
str ends with 'string'<br> |
|||
false<br> |
|||
true<br> |
|||
"This is another string"<br> |
|||
"This is even another string"<br> |
|||
"This is even ANOTHER string"<br> |
|||
=={{header|Emacs Lisp}}== |
|||
<syntaxhighlight lang="lisp">(let ((string "I am a string")) |
|||
(when (string-match-p "string$" string) |
|||
(message "Ends with 'string'")) |
|||
(message "%s" (replace-regexp-in-string " a " " another " string)))</syntaxhighlight> |
|||
{{out}} |
|||
Ends with 'string' |
|||
I am another string |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">match() -> |
||
String = "This is a string", |
String = "This is a string", |
||
case re:run(String, "string$") of |
case re:run(String, "string$") of |
||
Line 456: | Line 876: | ||
String = "This is a string", |
String = "This is a string", |
||
NewString = re:replace(String, " a ", " another ", [{return, list}]), |
NewString = re:replace(String, " a ", " another ", [{return, list}]), |
||
io:format("~s~n",[NewString]).</ |
io:format("~s~n",[NewString]).</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
|||
{{trans|C#}} |
|||
<syntaxhighlight lang="fsharp">open System |
|||
open System.Text.RegularExpressions |
|||
[<EntryPoint>] |
|||
let main argv = |
|||
let str = "I am a string" |
|||
if Regex("string$").IsMatch(str) then Console.WriteLine("Ends with string.") |
|||
let rstr = Regex(" a ").Replace(str, " another ") |
|||
Console.WriteLine(rstr) |
|||
0</syntaxhighlight> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang="factor">USING: io kernel prettyprint regexp ; |
|||
IN: rosetta-code.regexp |
|||
"1000000" R/ 10+/ matches? . ! Does the entire string match the regexp? |
|||
"1001" R/ 10+/ matches? . |
|||
"1001" R/ 10+/ re-contains? . ! Does the string contain the regexp anywhere? |
|||
"blueberry pie" R/ \p{alpha}+berry/ "pumpkin" re-replace print</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
t |
|||
f |
|||
t |
|||
pumpkin pie |
|||
</pre> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{libheader|Forth Foundation Library}} |
{{libheader|Forth Foundation Library}} |
||
Test/Match |
Test/Match |
||
< |
<syntaxhighlight lang="forth">include ffl/rgx.fs |
||
\ Create a regular expression variable 'exp' in the dictionary |
\ Create a regular expression variable 'exp' in the dictionary |
||
Line 479: | Line 930: | ||
[ELSE] |
[ELSE] |
||
.( No match.) cr |
.( No match.) cr |
||
[THEN]</ |
[THEN]</syntaxhighlight> |
||
=={{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}}== |
=={{header|Frink}}== |
||
Pattern matching: |
Pattern matching: |
||
< |
<syntaxhighlight lang="frink"> |
||
line = "My name is Inigo Montoya." |
line = "My name is Inigo Montoya." |
||
Line 492: | Line 960: | ||
println["Last name is: $last"] |
println["Last name is: $last"] |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Replacement: (Replaces in the variable <code>line</code>) |
Replacement: (Replaces in the variable <code>line</code>) |
||
< |
<syntaxhighlight lang="frink"> |
||
line =~ %s/Frank/Frink/g |
line =~ %s/Frank/Frink/g |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=5a2a1052f8dff12596fa7f45242d25a9 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">Public Sub Main() |
|||
Dim sString As String = "Hello world!" |
|||
If sString Ends "!" Then Print sString & " ends with !" |
|||
If sString Begins "Hel" Then Print sString & " begins with 'Hel'" |
|||
sString = Replace(sString, "world", "moon") |
|||
Print sString |
|||
End </syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
Hello world! ends with ! |
|||
Hello world! begins with 'Hel' |
|||
Hello moon! |
|||
</pre> |
|||
=={{header|GeneXus}}== |
|||
Interesting link: http://wiki.gxtechnical.com/commwiki/servlet/hwiki?Regular+Expressions+%28RegEx%29,<br /> |
|||
<br /> |
|||
Replacement:<br /> |
|||
<syntaxhighlight lang="genexus">&string = &string.ReplaceRegEx("^\s+|\s+$", "") // it's a trim! |
|||
&string = &string.ReplaceRegEx("Another (Match)", "Replacing $1") // Using replace groups</syntaxhighlight> |
|||
Check match: |
|||
<syntaxhighlight lang="genexus">If (&string.IsMatch("regex$")) |
|||
// The string ends with "regex" |
|||
EndIf</syntaxhighlight> |
|||
Split RegEx: |
|||
<syntaxhighlight lang="genexus">&stringCollection = &string.SplitRegEx("^\d{2,4}")</syntaxhighlight> |
|||
Matches: |
|||
<syntaxhighlight lang="genexus">&RegExMatchCollection = &string.Matches("(pa)tt(ern)") |
|||
For &RegExMatch In &RegExMatchCollection |
|||
&FullMatch = &RegExMatch.Value // &FullMatch contains the full pattern match: "pattern" |
|||
For &matchVarchar In &RegExMatch.Groups |
|||
// &matchVarchar contains group matches: "pa", "ern" |
|||
EndFor |
|||
EndFor</syntaxhighlight> |
|||
Flags: <br /> |
|||
s - Dot matches all (including newline) <br /> |
|||
m - multiline <br /> |
|||
i - ignore case <br /> |
|||
Using Flags Sintax: (?flags)pattern <br /> |
|||
Example:<br /> |
|||
<syntaxhighlight lang="genexus">&string = &string.ReplaceRegEx("(?si)IgnoreCase.+$", "") // Flags s and i</syntaxhighlight> |
|||
Error Handling: |
|||
<syntaxhighlight 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()</syntaxhighlight> |
|||
=={{header|Genie}}== |
|||
<syntaxhighlight lang="genie">[indent=4] |
|||
/* Regular expressions, in Genie */ |
|||
init |
|||
var sentence = "This is a sample sentence." |
|||
try |
|||
var re = new Regex("s[ai]mple") |
|||
if re.match(sentence) |
|||
print "matched '%s' in '%s'", re.get_pattern(), sentence |
|||
var offs = 0 |
|||
print("replace with 'different': %s", |
|||
re.replace(sentence, sentence.length, offs, "different")) |
|||
except err:RegexError |
|||
print err.message</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ valac regularExpressions.gs |
|||
prompt$ ./regularExpressions |
|||
matched 's[ai]mple' in 'This is a sample sentence.' |
|||
replace with 'different': This is a different sentence.</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
import "regexp" |
import "regexp" |
||
Line 515: | Line 1,061: | ||
result := pattern.ReplaceAllString(str, "modified") |
result := pattern.ReplaceAllString(str, "modified") |
||
fmt.Println(result) |
fmt.Println(result) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
"Matching" Solution (it's complicated): |
"Matching" Solution (it's complicated): |
||
< |
<syntaxhighlight lang="groovy">import java.util.regex.*; |
||
def woodchuck = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" |
def woodchuck = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" |
||
Line 572: | Line 1,118: | ||
println ("'${woodchuck}' ${wwNotMatches ? 'does not' : 'does'} match '${woodRE}' exactly") |
println ("'${woodchuck}' ${wwNotMatches ? 'does not' : 'does'} match '${woodRE}' exactly") |
||
println ("'${woodchuck}' ${wwMatches ? 'does' : 'does not'} match '${containsWoodRE}' exactly")</ |
println ("'${woodchuck}' ${wwMatches ? 'does' : 'does not'} match '${containsWoodRE}' exactly")</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>=== Regular-expression String syntax (/string/)=== |
<pre>=== Regular-expression String syntax (/string/)=== |
||
[woodRE:[Ww]o\w+d, piperRE:[Pp]\w+r] |
[woodRE:[Ww]o\w+d, piperRE:[Pp]\w+r] |
||
Line 592: | Line 1,138: | ||
Replacement Solution (String.replaceAll()): |
Replacement Solution (String.replaceAll()): |
||
< |
<syntaxhighlight lang="groovy">println woodchuck.replaceAll(/c\w+k/, "CHUCK")</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood?</pre> |
<pre>How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood?</pre> |
||
Reusable Replacement Solution (Matcher.replaceAll()): |
Reusable Replacement Solution (Matcher.replaceAll()): |
||
< |
<syntaxhighlight lang="groovy">def ck = (woodchuck =~ /c\w+k/) |
||
println (ck.replaceAll("CHUCK")) |
println (ck.replaceAll("CHUCK")) |
||
println (ck.replaceAll("wind")) |
println (ck.replaceAll("wind")) |
||
Line 608: | Line 1,154: | ||
println (ck.replaceAll("man")) |
println (ck.replaceAll("man")) |
||
println (ck.replaceAll("work")) |
println (ck.replaceAll("work")) |
||
println (ck.replaceAll("pickle"))</ |
println (ck.replaceAll("pickle"))</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood? |
<pre>How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood? |
||
How much wood would a woodwind wind if a woodwind could wind wood? |
How much wood would a woodwind wind if a woodwind could wind wood? |
||
Line 624: | Line 1,170: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Test |
Test |
||
< |
<syntaxhighlight lang="haskell">import Text.Regex |
||
str = "I am a string" |
str = "I am a string" |
||
Line 630: | Line 1,176: | ||
case matchRegex (mkRegex ".*string$") str of |
case matchRegex (mkRegex ".*string$") str of |
||
Just _ -> putStrLn $ "ends with 'string'" |
Just _ -> putStrLn $ "ends with 'string'" |
||
Nothing -> return ()</ |
Nothing -> return ()</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="haskell">import Text.Regex |
||
orig = "I am the original string" |
orig = "I am the original string" |
||
result = subRegex (mkRegex "original") orig "modified" |
result = subRegex (mkRegex "original") orig "modified" |
||
putStrLn $ result</ |
putStrLn $ result</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER string*100/ "The quick brown fox jumps over the lazy dog" / |
||
REAL, PARAMETER :: Regex=128, Count=256 |
REAL, PARAMETER :: Regex=128, Count=256 |
||
Line 646: | Line 1,192: | ||
vocals_changed = EDIT(Text=string, Option=Regex, Right="[aeiou]", RePLaceby='**', DO=LEN(string) ) ! changes 11 |
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</ |
WRITE(ClipBoard) string ! Th** q****ck br**wn f**x j**mps **v**r th** l**zy d**g</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Regex includes procedures to provide access to regular expressions within native string scanning and matching expressions. |
Regex includes procedures to provide access to regular expressions within native string scanning and matching expressions. |
||
'ReFind' and 'ReMatch' respectively generate the sequence of beginning and ending positions matched by a regular expression. |
|||
Additionally, there is a regular expression pattern compiler 'RePat' and other supporting functions and variables. |
|||
< |
<syntaxhighlight lang="icon">procedure main() |
||
s := "A simple string" |
s := "A simple string" |
||
Line 662: | Line 1,210: | ||
end |
end |
||
link regexp # link to IPL regexp </ |
link regexp # link to IPL regexp </syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
[http://www.cs.arizona.edu/icon/library/procs/regexp.htm See regexp]. |
[http://www.cs.arizona.edu/icon/library/procs/regexp.htm See regexp]. |
||
{{out}} |
|||
Sample output:<pre>"A simple string" matches "string$" |
|||
<pre>"A simple string" matches "string$" |
|||
"A simple replacement"</pre> |
"A simple replacement"</pre> |
||
Line 672: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="inform7">let T be indexed text; |
||
let T be "A simple string"; |
let T be "A simple string"; |
||
if T matches the regular expression ".*string$", say "ends with string."; |
if T matches the regular expression ".*string$", say "ends with string."; |
||
replace the regular expression "simple" in T with "replacement";</ |
replace the regular expression "simple" in T with "replacement";</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 681: | Line 1,230: | ||
J's regex support is built on top of PCRE. |
J's regex support is built on top of PCRE. |
||
< |
<syntaxhighlight lang="j">load'regex' NB. Load regex library |
||
str =: 'I am a string' NB. String used in examples.</ |
str =: 'I am a string' NB. String used in examples.</syntaxhighlight> |
||
Matching: |
Matching: |
||
< |
<syntaxhighlight lang="j"> '.*string$' rxeq str NB. 1 is true, 0 is false |
||
1</ |
1</syntaxhighlight> |
||
Substitution: |
Substitution: |
||
< |
<syntaxhighlight lang="j"> ('am';'am still') rxrplc str |
||
I am still a string</ |
I am still a string</syntaxhighlight> |
||
Note: use<lang |
Note: use<syntaxhighlight lang="j"> open'regex'</syntaxhighlight> to read the source code for the library. The comments list 6 main definitions and a dozen utility definitions. |
||
=={{header|Java}}== |
=={{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+}} |
{{works with|Java|1.4+}} |
||
Test |
Test |
||
< |
<syntaxhighlight lang="java">String str = "I am a string"; |
||
if (str.matches(".*string")) { // note: matches() tests if the entire string is a match |
if (str.matches(".*string")) { // note: matches() tests if the entire string is a match |
||
System.out.println("ends with 'string'"); |
System.out.println("ends with 'string'"); |
||
}</ |
}</syntaxhighlight> |
||
To match part of a string, or to process matches: |
To match part of a string, or to process matches: |
||
< |
<syntaxhighlight lang="java">import java.util.regex.*; |
||
Pattern p = Pattern.compile("a*b"); |
Pattern p = Pattern.compile("a*b"); |
||
Matcher m = p.matcher(str); |
Matcher m = p.matcher(str); |
||
while (m.find()) { |
while (m.find()) { |
||
// use m.group() to extract matches |
// use m.group() to extract matches |
||
}</ |
}</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="java">String orig = "I am the original string"; |
||
String result = orig.replaceAll("original", "modified"); |
String result = orig.replaceAll("original", "modified"); |
||
// result is now "I am the modified string"</ |
// result is now "I am the modified string"</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Test/Match |
Test/Match |
||
< |
<syntaxhighlight lang="javascript">var subject = "Hello world!"; |
||
// Two different ways to create the RegExp object |
// Two different ways to create the RegExp object |
||
// Both examples use the exact same pattern... matching "hello" |
// Both examples use the exact same pattern... matching "hello " |
||
var re_PatternToMatch = /Hello (World)/i; // creates a RegExp literal with case-insensitivity |
var re_PatternToMatch = /Hello (World)/i; // creates a RegExp literal with case-insensitivity |
||
var re_PatternToMatch2 = new RegExp("Hello (World)", "i"); |
var re_PatternToMatch2 = new RegExp("Hello (World)", "i"); |
||
Line 735: | Line 1,313: | ||
// matches[0] == "Hello world" |
// matches[0] == "Hello world" |
||
// matches[1] == "world" |
// matches[1] == "world" |
||
var matches = re_PatternToMatch2.exec(subject);</ |
var matches = re_PatternToMatch2.exec(subject);</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="javascript">var subject = "Hello world!"; |
||
// Perform a string replacement |
// Perform a string replacement |
||
// newSubject == "Replaced!" |
// newSubject == "Replaced!" |
||
var newSubject = subject.replace(re_PatternToMatch, "Replaced");</ |
var newSubject = subject.replace(re_PatternToMatch, "Replaced");</syntaxhighlight> |
||
=={{header|jq}}== |
|||
{{works with|jq|with regex support}} |
|||
Recent versions of jq (jq > 1.4) include PCRE regex support using the [http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt Oniguruma library]. |
|||
'''Test''': |
|||
<syntaxhighlight lang="jq">"I am a string" | test("string$") </syntaxhighlight> |
|||
yields: true |
|||
'''Substitutution''': |
|||
<syntaxhighlight lang="jq">"I am a string" | sub(" a "; " another ")</syntaxhighlight> |
|||
yields: "I am another string" |
|||
'''Substitution using capture''': |
|||
<syntaxhighlight lang="jq">"abc" | sub( "(?<head>^.)(?<tail>.*)"; "\(.head)-\(.tail)")</syntaxhighlight> |
|||
yields: "a-bc" |
|||
=={{header|Jsish}}== |
|||
<syntaxhighlight lang="javascript">/* Regular expressions, in Jsish */ |
|||
var re = /s[ai]mple/; |
|||
var sentence = 'This is a sample sentence'; |
|||
var matches = sentence.match(re); |
|||
if (matches.length > 0) printf('%s found in "%s" using %q\n', matches[0], sentence, re); |
|||
var replaced = sentence.replace(re, "different"); |
|||
printf("replaced sentence is: %s\n", replaced);</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ jsish regularExpressions.jsi |
|||
sample found in "This is a sample sentence" using "/s[ai]mple/" |
|||
replaced sentence is: This is a different sentence</pre> |
|||
=={{header|Julia}}== |
|||
{{trans|Perl}} |
|||
Julia implements Perl-compatible regular expressions (via the built-in [http://www.pcre.org/ PCRE library]). To test for a match: |
|||
<syntaxhighlight lang="julia">s = "I am a string" |
|||
if ismatch(r"string$", s) |
|||
println("'$s' ends with 'string'") |
|||
end</syntaxhighlight> |
|||
To perform replacements: |
|||
<syntaxhighlight lang="julia">s = "I am a string" |
|||
s = replace(s, r" (a|an) ", " another ")</syntaxhighlight> |
|||
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}}== |
|||
<syntaxhighlight lang="scala">// version 1.0.6 |
|||
fun main(args: Array<String>) { |
|||
val s1 = "I am the original string" |
|||
val r1 = Regex("^.*string$") |
|||
if (s1.matches(r1)) println("`$s1` matches `$r1`") |
|||
val r2 = Regex("original") |
|||
val s3 = "replacement" |
|||
val s2 = s1.replace(r2, s3) |
|||
if (s2 != s1) println("`$s2` replaces `$r2` with `$s3`") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
`I am the original string` matches `^.*string$` |
|||
`I am the replacement string` replaces `original` with `replacement` |
|||
</pre> |
|||
=={{header|langur}}== |
|||
The following examples use re2 regex literals. |
|||
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 (->). |
|||
To match a string, ... |
|||
<syntaxhighlight lang="langur">if "somestring" -> re/abc/ { ... }</syntaxhighlight> |
|||
Or... |
|||
<syntaxhighlight lang="langur">if x, y = submatch(re/(abc+).+?(def)/, "somestring") { ... }</syntaxhighlight> |
|||
Or... |
|||
<syntaxhighlight lang="langur">switch "somestring" { |
|||
case -> re/abc/: ... |
|||
... |
|||
}</syntaxhighlight> |
|||
Or... |
|||
<syntaxhighlight lang="langur">switch -> re/abc/ { |
|||
case "somestring": ... |
|||
... |
|||
}</syntaxhighlight> |
|||
Substitution does not alter the original string. |
|||
<syntaxhighlight lang="langur">replace("abcdef", re/abc/, "Y") |
|||
# result: "Ydef"</syntaxhighlight> |
|||
=={{header|Lasso}}== |
|||
Lasso has built in support for regular expressions using ICU regexps. |
|||
<syntaxhighlight lang="lasso">local(mytext = 'My name is: Stone, Rosetta |
|||
My name is: Hippo, Campus |
|||
') |
|||
local(regexp = regexp( |
|||
-find = `(?m)^My name is: (.*?), (.*?)$`, |
|||
-input = #mytext, |
|||
-replace = `Hello! I am $2 $1`, |
|||
-ignorecase |
|||
)) |
|||
while(#regexp -> find) => {^ |
|||
#regexp -> groupcount > 1 ? (#regexp -> matchString(2) -> trim&) + '<br />' |
|||
^} |
|||
#regexp -> reset(-input = #mytext) |
|||
#regexp -> findall |
|||
#regexp -> reset(-input = #mytext) |
|||
'<br />' |
|||
#regexp -> replaceall</syntaxhighlight> |
|||
<pre>Rosetta |
|||
Campus |
|||
array(My name is: Stone, Rosetta, My name is: Hippo, Campus) |
|||
Hello! I am Rosetta Stone Hello! I am Campus Hippo</pre> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<lang lua>str1 = "This is a string!" |
|||
str2 = "string" |
|||
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. |
|||
print( str1:match( str2 ) ) |
|||
erg = str1:gsub( "a", "another" ); print( erg )</lang> |
|||
<syntaxhighlight lang="lua">test = "My name is Lua." |
|||
pattern = ".*name is (%a*).*" |
|||
if test:match(pattern) then |
|||
print("Name found.") |
|||
end |
|||
sub, num_matches = test:gsub(pattern, "Hello, %1!") |
|||
print(sub)</syntaxhighlight> |
|||
=={{header|M2000 Interpreter}}== |
|||
We can use COM objects so we can use VBscript.RegExp |
|||
Properties Count and List() are bound with MyMatches using smart pointer. So MyMatches can change object, and Count and List() can operate with new object. |
|||
Com objects always have one real pointer. We can't returned id (because at the exit of module or function where we create it pointer get Nothing, so properties have a "broken" pointer, and return of object returns a broken smart pointer). We can pass by reference com objects, or we can use it as Globals (until "mother" module finish). We can use WithEvent if we wish to get events. We have to make functions with object_event name (note that forms event use dot not underscore). |
|||
We can use named parameters (for Word) Try {Method Documents, "add", "", DocumentType:=WdNewWebPage as doc1} |
|||
New version using enumerator from object. Including a help SUB for displaying all functions of a COM object. |
|||
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"> |
|||
Module CheckIt { |
|||
declare global ObjRegEx "VBscript.RegExp" |
|||
Function RegEx.Replace$(from$, what$) { |
|||
Method ObjRegEx, "Replace", from$, what$ as response$ |
|||
=response$ |
|||
} |
|||
Function RegEx.Test(what$) { |
|||
Method ObjRegEx, "Test", what$ as response |
|||
=response |
|||
} |
|||
Print Type$(ObjRegEx) |
|||
With ObjRegEx, "Global", True, "Pattern" as pattern$ |
|||
pattern$="Mona Lisa" |
|||
Print RegEx.Test("The Mona Lisa is in the Louvre.")=true |
|||
Print RegEx.Replace$("The Mona Lisa is in the Louvre.", "La Gioconda") |
|||
Pattern$ = " {2,}" |
|||
Print "Myer Ken, Vice President, Sales and Services" |
|||
\\ Removing some spaces |
|||
Print RegEx.Replace$("Myer Ken, Vice President, Sales and Services", " ") |
|||
pattern$="(\d{3})-(\d{3})-(\d{4})" |
|||
Method ObjRegEx, "Execute", "555-123-4567, 555-943-6717" as MyMatches |
|||
Print Type$(MyMatches) ' it is a IMatchCollection2 |
|||
With MyMatches, "Count" as count, "Item" as List$() |
|||
For i=0 to Count-1 : Print List$(i) : Next i |
|||
Print RegEx.Replace$("555-123-4567, 555-943-6717", "($1) $2-$3") |
|||
Pattern$ = "(\S+), (\S+)" |
|||
Print RegEx.Replace$("Myer, Ken", "$2 $1") |
|||
Method ObjRegEx, "Execute", "Myer, Ken" as MyMatches |
|||
Rem : DisplayFunctions(MyMatches) |
|||
\\ we can use Enumerator |
|||
With MyMatches, "_NewEnum" as New Matches |
|||
Rem : DisplayFunctions(Matches) |
|||
With Matches, "Value" as New item$ |
|||
While Matches { |
|||
Print Item$ |
|||
} |
|||
\\ Or just using the list$() |
|||
For i=0 to Count-1 : Print List$(i) : Next i |
|||
declare ObjRegEx Nothing |
|||
End |
|||
Sub DisplayFunctions(x) |
|||
Local cc=param(x), ec=each(cc) |
|||
while ec { |
|||
Print eval$(ec) ' print every function/property of object x |
|||
} |
|||
End Sub |
|||
} |
|||
Checkit |
|||
\\ internal has no pattern. There is a like operator (~) for strings which use pattern matching (using VB6 like). We can use Instr() and RInstr() for strings. |
|||
Module Internal { |
|||
what$="Mona Lisa" |
|||
Document a$="The Mona Lisa is in the Louvre." |
|||
Find a$, what$ |
|||
Read FindWhere |
|||
If FindWhere<>0 then Read parNo, parlocation |
|||
\\ replace in place |
|||
Insert FindWhere, Len(what$) a$="La Gioconda" |
|||
Report a$ |
|||
n$="The Mona Lisa is in the Louvre, not the Mona Lisa" |
|||
Report Replace$("Mona Lisa", "La Gioconda", n$, 1, 1) ' replace from start only one |
|||
dim a$() |
|||
a$()=Piece$("Myer, Ken",", ") |
|||
Print a$(1)+", "+a$(0)="Ken, Myer" |
|||
} |
|||
Internal |
|||
</syntaxhighlight> |
|||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">regexp(`GNUs not Unix', `\<[a-z]\w+') |
||
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')</ |
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
5 |
5 |
||
a not b ot c |
a not b ot c |
||
</pre> |
</pre> |
||
=={{header|Mathematica}}== |
|||
=={{header|Maple}}== |
|||
<lang Mathematica> |
|||
<syntaxhighlight lang="maple">#Examples from Maple Help |
|||
StringCases["I am a string with the number 18374 in me",RegularExpression["[0-9]+"]] |
|||
StringTools:-RegMatch("^ab+bc$", "abbbbc"); |
|||
StringReplace["I am a string",RegularExpression["I\\sam"] -> "I'm"] |
|||
StringTools:-RegMatch("^ab+bc$", "abbbbcx"); |
|||
</lang> |
|||
StringTools:-RegSub("a([bc]*)(c*d)", "abcd", "&-\\1-\\2"); |
|||
StringTools:-RegSub("(.*)c(anad[ai])(.*)", "Maple is canadian", "\\1C\\2\\3");</syntaxhighlight> |
|||
{{Out|Output}} |
|||
<pre>true |
|||
false |
|||
"abcd-bc-d" |
|||
"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]+"]] |
|||
StringReplace["I am a string",RegularExpression["I\\sam"] -> "I'm"]</syntaxhighlight> |
|||
The in-notebook output, in order: |
The in-notebook output, in order: |
||
<pre>{18374} |
|||
I'm a string</pre> |
|||
=={{header|MAXScript}}== |
|||
<syntaxhighlight lang="maxscript"> |
|||
samples = #("Some string 123","Example text 123","string",\ |
|||
"ThisString Will Not Match","A123,333,string","123451") |
|||
samples2 = #("I am a string","Me too.") |
|||
regex = dotnetobject "System.Text.RegularExpressions.Regex" ".*\bstring*" |
|||
regex2 = dotnetobject "System.Text.RegularExpressions.Regex" "\ba\b" |
|||
clearlistener() |
|||
format "Pattern is : %\n" (regex.toString()) |
|||
for i in samples do |
|||
( |
|||
if regex.ismatch(i) then |
|||
( |
|||
format "The string \"%\" matches the pattern\n" i |
|||
) |
|||
else |
|||
( |
|||
format "The string \"%\" doesn't match the pattern\n" i |
|||
) |
|||
) |
|||
-- replacement |
|||
format "Pattern is : %\n" (regex2.toString()) |
|||
for i in samples2 do |
|||
( |
|||
if regex2.ismatch(i) then |
|||
( |
|||
local replaced = regex2.replace i "another" |
|||
format "The string \"%\" matched the pattern, so it was replaced: \"%\"\n" i replaced |
|||
) |
|||
else |
|||
( |
|||
format "The string \"%\" does not match the pattern\n" i |
|||
) |
|||
) |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
<pre> |
||
OK |
|||
{18374} |
|||
Pattern is : .*\bstring* |
|||
I'm a string |
|||
OK |
|||
The string "Some string 123" matches the pattern |
|||
The string "Example text 123" doesn't match the pattern |
|||
The string "string" matches the pattern |
|||
The string "ThisString Will Not Match" doesn't match the pattern |
|||
The string "A123,333,string" matches the pattern |
|||
The string "123451" doesn't match the pattern |
|||
OK |
|||
Pattern is : \ba\b |
|||
OK |
|||
The string "I am a string" matched the pattern, so it was replaced: "I am another string" |
|||
The string "And me too." does not match the pattern |
|||
OK |
|||
OK |
|||
</pre> |
</pre> |
||
=={{header|MIRC Scripting Language}}== |
=={{header|MIRC Scripting Language}}== |
||
< |
<syntaxhighlight lang="mirc">alias regular_expressions { |
||
var %string = This is a string |
var %string = This is a string |
||
var %re = string$ |
var %re = string$ |
||
Line 784: | Line 1,649: | ||
%re = \b(another)\b |
%re = \b(another)\b |
||
echo -a Result 2: $regsubex(%string,%re,yet \1) |
echo -a Result 2: $regsubex(%string,%re,yet \1) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Ends with string. |
Ends with string. |
||
Line 794: | Line 1,659: | ||
=={{header|MUMPS}}== |
=={{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>< |
<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><syntaxhighlight lang="mumps">REGEXP |
||
NEW HI,W,PATTERN,BOOLEAN |
NEW HI,W,PATTERN,BOOLEAN |
||
SET HI="Hello, world!",W="world" |
SET HI="Hello, world!",W="world" |
||
Line 806: | Line 1,671: | ||
SET BOOLEAN=$FIND(HI,W) |
SET BOOLEAN=$FIND(HI,W) |
||
IF BOOLEAN>0 WRITE $PIECE(HI,W,1)_"string"_$PIECE(HI,W,2) |
IF BOOLEAN>0 WRITE $PIECE(HI,W,1)_"string"_$PIECE(HI,W,2) |
||
QUIT</ |
QUIT</syntaxhighlight> |
||
Usage:<pre> |
Usage:<pre> |
||
USER>D REGEXP^ROSETTA |
USER>D REGEXP^ROSETTA |
||
Line 816: | Line 1,681: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 831: | Line 1,696: | ||
say 'String "'st1'"' 'matches pattern "'rx1ef'":' Boolean(mcm) |
say 'String "'st1'"' 'matches pattern "'rx1ef'":' Boolean(mcm) |
||
say |
say |
||
say 'Replace all |
say 'Replace all occurrences of regex pattern "'rx1ef'" with "'sbx'"' |
||
stx = Rexx |
stx = Rexx |
||
stx = (String st1).replaceAll(rx1ef, sbx) |
stx = (String st1).replaceAll(rx1ef, sbx) |
||
Line 845: | Line 1,710: | ||
mc1 = pt1.matcher(st1) |
mc1 = pt1.matcher(st1) |
||
say |
say |
||
say 'Replace all |
say 'Replace all occurrences of regex pattern "'rx1'" with "'sbx'"' |
||
sx1 = Rexx |
sx1 = Rexx |
||
sx1 = mc1.replaceAll(sbx) |
sx1 = mc1.replaceAll(sbx) |
||
Line 853: | Line 1,718: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
'''Output:''' |
|||
<pre> |
<pre> |
||
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "(?i)f.e.*?": true |
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "(?i)f.e.*?": true |
||
Replace all |
Replace all occurrences of regex pattern "(?i)f.e.*?" with "foo" |
||
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" |
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" |
||
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" |
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" |
||
Line 864: | Line 1,729: | ||
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "f.e.*?": true |
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "f.e.*?": true |
||
Replace all |
Replace all occurrences of regex pattern "f.e.*?" with "foo" |
||
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" |
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" |
||
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" |
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" |
||
</pre> |
</pre> |
||
=={{header|NewLISP |
=={{header|NewLISP}}== |
||
<lang |
<syntaxhighlight lang="newlisp ">(regex "[bB]+" "AbBBbABbBAAAA") -> ("bBBb" 1 4)</syntaxhighlight> |
||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">import re |
|||
var s = "This is a string" |
|||
if s.find(re"string$") > -1: |
|||
echo "Ends with string." |
|||
s = s.replace(re"\ a\ ", " another ") |
|||
echo s</syntaxhighlight> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
use RegEx; |
use RegEx; |
||
Line 892: | Line 1,767: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 898: | Line 1,773: | ||
{{works with|Mac OS X|10.4+}} |
{{works with|Mac OS X|10.4+}} |
||
{{works with|iOS|3.0+}} |
{{works with|iOS|3.0+}} |
||
< |
<syntaxhighlight lang="objc">NSString *str = @"I am a string"; |
||
NSString *regex = @".*string$"; |
NSString *regex = @".*string$"; |
||
Line 906: | Line 1,781: | ||
if ([pred evaluateWithObject:str]) { |
if ([pred evaluateWithObject:str]) { |
||
NSLog(@"ends with 'string'"); |
NSLog(@"ends with 'string'"); |
||
}</ |
}</syntaxhighlight> |
||
Unfortunately this method cannot find the location of the match or do substitution. |
Unfortunately this method cannot find the location of the match or do substitution. |
||
Line 913: | Line 1,788: | ||
{{works with|Mac OS X|10.7+}} |
{{works with|Mac OS X|10.7+}} |
||
{{works with|iOS|3.2+}} |
{{works with|iOS|3.2+}} |
||
< |
<syntaxhighlight lang="objc">NSString *str = @"I am a string"; |
||
if ([str rangeOfString:@"string$" options:NSRegularExpressionSearch].location != NSNotFound) { |
if ([str rangeOfString:@"string$" options:NSRegularExpressionSearch].location != NSNotFound) { |
||
NSLog(@"Ends with 'string'"); |
NSLog(@"Ends with 'string'"); |
||
}</ |
}</syntaxhighlight> |
||
Substitute |
Substitute |
||
{{works with|Mac OS X|10.7+}} |
{{works with|Mac OS X|10.7+}} |
||
{{works with|iOS|4.0+}} undocumented |
{{works with|iOS|4.0+}} undocumented |
||
< |
<syntaxhighlight lang="objc">NSString *orig = @"I am the original string"; |
||
NSString *result = [orig stringByReplacingOccurrencesOfString:@"original" |
NSString *result = [orig stringByReplacingOccurrencesOfString:@"original" |
||
withString:@"modified" |
withString:@"modified" |
||
options:NSRegularExpressionSearch |
options:NSRegularExpressionSearch |
||
range:NSMakeRange(0, [orig length])]; |
range:NSMakeRange(0, [orig length])]; |
||
NSLog(@"%@", result);</ |
NSLog(@"%@", result);</syntaxhighlight> |
||
===NSRegularExpression=== |
===NSRegularExpression=== |
||
Line 932: | Line 1,807: | ||
{{works with|iOS|4.0+}} |
{{works with|iOS|4.0+}} |
||
Test |
Test |
||
< |
<syntaxhighlight lang="objc">NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"string$" |
||
options:0 |
options:0 |
||
error:NULL]; |
error:NULL]; |
||
Line 941: | Line 1,816: | ||
].location != NSNotFound) { |
].location != NSNotFound) { |
||
NSLog(@"Ends with 'string'"); |
NSLog(@"Ends with 'string'"); |
||
}</ |
}</syntaxhighlight> |
||
Loop through matches |
Loop through matches |
||
< |
<syntaxhighlight lang="objc">for (NSTextCheckingResult *match in [regex matchesInString:str |
||
options:0 |
options:0 |
||
range:NSMakeRange(0, [str length]) |
range:NSMakeRange(0, [str length]) |
||
Line 950: | Line 1,825: | ||
// match.range gives the range of the whole match |
// match.range gives the range of the whole match |
||
// [match rangeAtIndex:i] gives the range of the i'th capture group (starting from 1) |
// [match rangeAtIndex:i] gives the range of the i'th capture group (starting from 1) |
||
}</ |
}</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="objc">NSString *orig = @"I am the original string"; |
||
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"original" |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"original" |
||
options:0 |
options:0 |
||
Line 961: | Line 1,836: | ||
range:NSMakeRange(0, [orig length]) |
range:NSMakeRange(0, [orig length]) |
||
withTemplate:@"modified"]; |
withTemplate:@"modified"]; |
||
NSLog(@"%@", result);</ |
NSLog(@"%@", result);</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
=== With the standard library === |
=== With the standard library === |
||
Test |
Test |
||
< |
<syntaxhighlight lang="ocaml">#load "str.cma";; |
||
let str = "I am a string";; |
let str = "I am a string";; |
||
try |
try |
||
Line 972: | Line 1,847: | ||
print_endline "ends with 'string'" |
print_endline "ends with 'string'" |
||
with Not_found -> () |
with Not_found -> () |
||
;;</ |
;;</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="ocaml">#load "str.cma";; |
||
let orig = "I am the original string";; |
let orig = "I am the original string";; |
||
let result = Str.global_replace (Str.regexp "original") "modified" orig;; |
let result = Str.global_replace (Str.regexp "original") "modified" orig;; |
||
(* result is now "I am the modified string" *)</ |
(* result is now "I am the modified string" *)</syntaxhighlight> |
||
=== Using Pcre === |
=== Using Pcre === |
||
'''Library:''' [http://ocaml.info/home/ocaml_sources.html#pcre-ocaml ocaml-pcre] |
'''Library:''' [http://ocaml.info/home/ocaml_sources.html#pcre-ocaml ocaml-pcre] |
||
< |
<syntaxhighlight lang="ocaml">let matched pat str = |
||
try ignore(Pcre.exec ~pat str); (true) |
try ignore(Pcre.exec ~pat str); (true) |
||
with Not_found -> (false) |
with Not_found -> (false) |
||
Line 992: | Line 1,867: | ||
Printf.printf "Substitute: %s\n" |
Printf.printf "Substitute: %s\n" |
||
(Pcre.replace ~pat:"original" ~templ:"modified" "I am the original string") |
(Pcre.replace ~pat:"original" ~templ:"modified" "I am the original string") |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Ol}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
; matching: |
|||
(define regex (string->regex "m/aa(bb|cc)dd/")) |
|||
(print (regex "aabbddx")) ; => true |
|||
(print (regex "aaccddx")) ; => true |
|||
(print (regex "aabcddx")) ; => false |
|||
; substitute part of a string: |
|||
(define regex (string->regex "s/aa(bb|cc)dd/HAHAHA/")) |
|||
(print (regex "aabbddx")) ; => HAHAHAx |
|||
(print (regex "aaccddx")) ; => HAHAHAx |
|||
(print (regex "aabcddx")) ; => false |
|||
</syntaxhighlight> |
|||
=={{header|ooRexx}}== |
|||
<syntaxhighlight lang="oorexx">/* Rexx */ |
|||
/* Using the RxRegExp Regular Expression built-in utility class */ |
|||
st1 = 'Fee, fie, foe, fum, I smell the blood of an Englishman' |
|||
rx1 = '[Ff]?e' -- unlike most regex engines, RxRegExp uses '?' instead of '.' to match any single character |
|||
sbx = 'foo' |
|||
myRE = .RegularExpression~new() |
|||
myRE~parse(rx1, MINIMAL) |
|||
mcm = myRE~pos(st1) |
|||
say 'String "'st1'"' 'matches pattern "'rx1'":' bool2string(mcm > 0) |
|||
say |
|||
-- The RxRegExp package doesn't provide a replace capability so you must roll your own |
|||
st0 = st1 |
|||
loop label GREP forever |
|||
mcp = myRE~pos(st1) |
|||
if mcp > 0 then do |
|||
mpp = myRE~position |
|||
fnd = st1~substr(mcp, mpp - mcp + 1) |
|||
stx = st1~changestr(fnd, sbx, 1) |
|||
end |
|||
else leave GREP |
|||
st1 = stx |
|||
end GREP |
|||
say 'Input string: "'st0'"' |
|||
say 'Result string: "'stx'"' |
|||
return |
|||
exit |
|||
bool2string: |
|||
procedure |
|||
do |
|||
parse arg bv . |
|||
if bv then bx = 'true' |
|||
else bx = 'false' |
|||
return bx |
|||
end |
|||
exit |
|||
::requires "rxregexp.cls" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "[Ff]?e": true |
|||
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" |
|||
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" |
|||
</pre> |
|||
=={{header|Oxygene}}== |
=={{header|Oxygene}}== |
||
< |
<syntaxhighlight lang="oxygene"> |
||
// Match and Replace part of a string using a Regular Expression |
// Match and Replace part of a string using a Regular Expression |
||
// |
// |
||
Line 1,026: | Line 1,969: | ||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Produces: |
Produces: |
||
<pre> |
<pre> |
||
Line 1,034: | Line 1,977: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
[Regex] = {Module.link ['x-oz://contrib/regex']} |
[Regex] = {Module.link ['x-oz://contrib/regex']} |
||
String = "This is a string" |
String = "This is a string" |
||
Line 1,041: | Line 1,984: | ||
{System.showInfo "Ends with string."} |
{System.showInfo "Ends with string."} |
||
end |
end |
||
{System.showInfo {Regex.replace String " a " fun {$ _ _} " another " end}}</ |
{System.showInfo {Regex.replace String " a " fun {$ _ _} " another " end}}</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal"> |
||
// Match and Replace part of a string using a Regular Expression |
// Match and Replace part of a string using a Regular Expression |
||
// |
// |
||
Line 1,069: | Line 2,013: | ||
if r.Exec(myResult) then writeln(' contains ' + r.Match[0]); |
if r.Exec(myResult) then writeln(' contains ' + r.Match[0]); |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Produces: |
Produces: |
||
<pre> |
<pre> |
||
Line 1,075: | Line 2,019: | ||
I think that I am Nigel contains I am |
I think that I am Nigel contains I am |
||
I think that you are Nigel contains you are |
I think that you are Nigel contains you are |
||
</pre> |
|||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
begin |
|||
// text in tag (lazy quantification) |
|||
var s := '<tag>abc</tag> def <tag>ghi</tag>'; |
|||
foreach var m in s.Matches('(?<=<tag>)(.*?)(?=</tag>)') do |
|||
Println(m.Value, m.Index); |
|||
// take words in parentheses |
|||
s := 'one two three four five'; |
|||
Regex.Replace(s,'\w+','<$0>').Println; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
abc 5 |
|||
ghi 24 |
|||
<one> <two> <three> <four> <five> |
|||
</pre> |
</pre> |
||
Line 1,080: | Line 2,044: | ||
{{works with|Perl|5.8.8}} |
{{works with|Perl|5.8.8}} |
||
Test |
Test |
||
< |
<syntaxhighlight lang="perl">$string = "I am a string"; |
||
if ($string =~ /string$/) { |
if ($string =~ /string$/) { |
||
print "Ends with 'string'\n"; |
print "Ends with 'string'\n"; |
||
Line 1,087: | Line 2,051: | ||
if ($string !~ /^You/) { |
if ($string !~ /^You/) { |
||
print "Does not start with 'You'\n"; |
print "Does not start with 'You'\n"; |
||
}</ |
}</syntaxhighlight> |
||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="perl">$string = "I am a string"; |
||
$string =~ s/ a / another /; # makes "I am a string" into "I am another string" |
$string =~ s/ a / another /; # makes "I am a string" into "I am another string" |
||
print $string;</ |
print $string;</syntaxhighlight> |
||
In Perl 5.14+, you can return a new substituted string without altering the original string: |
In Perl 5.14+, you can return a new substituted string without altering the original string: |
||
< |
<syntaxhighlight lang="perl">$string = "I am a string"; |
||
$string2 = $string =~ s/ a / another /r; # $string2 == "I am another string", $string is unaltered |
$string2 = $string =~ s/ a / another /r; # $string2 == "I am another string", $string is unaltered |
||
print $string2;</ |
print $string2;</syntaxhighlight> |
||
Test and Substitute |
Test and Substitute |
||
< |
<syntaxhighlight lang="perl">$string = "I am a string"; |
||
if ($string =~ s/\bam\b/was/) { # \b is a word border |
if ($string =~ s/\bam\b/was/) { # \b is a word border |
||
print "I was able to find and replace 'am' with 'was'\n"; |
print "I was able to find and replace 'am' with 'was'\n"; |
||
}</ |
}</syntaxhighlight> |
||
Options |
Options |
||
< |
<syntaxhighlight lang="perl"># add the following just after the last / for additional control |
||
# g = globally (match as many as possible) |
# g = globally (match as many as possible) |
||
# i = case-insensitive |
# i = case-insensitive |
||
Line 1,115: | Line 2,079: | ||
# m = multi-line (the expression is run on each line individually) |
# 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"</ |
$string =~ s/i/u/ig; # would change "I am a string" into "u am a strung"</syntaxhighlight> |
||
Omission of the regular expression binding operators |
|||
=={{header|Perl 6}}== |
|||
<lang perl6>use v6; |
|||
if 'a long string' ~~ /string$/ { |
|||
say "It ends with 'string'"; |
|||
} |
|||
If regular expression matches are being made against the topic variable, it is possible to omit the regular expression binding operators: |
|||
# substitution has a few nifty features |
|||
<syntaxhighlight lang="perl">$_ = "I like banana milkshake."; |
|||
$_ = 'The quick Brown fox'; |
|||
if (/banana/) { # The regular expression binding operator is omitted |
|||
s:g:samecase/\w+/xxx/; |
|||
print "Match found\n"; |
|||
.say; |
|||
}</syntaxhighlight> |
|||
# output: |
|||
# Xxx xxx Xxx xxx |
|||
=={{header|Phix}}== |
|||
</lang> |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
<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> |
|||
"I am a string" ends with string |
|||
"I am a string" does not start with You |
|||
"ab**ef*" |
|||
"ab(C)(D)ef(G)(H)" |
|||
"ab(CD)ef(GH)" |
|||
"I am a replacement" |
|||
"I am another string" |
|||
"I am another replacement" |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|PHP|5.2.0}} |
{{works with|PHP|5.2.0}} |
||
< |
<syntaxhighlight lang="php">$string = 'I am a string'; |
||
# Test |
# Test |
||
if (preg_match('/string$/', $string)) |
if (preg_match('/string$/', $string)) |
||
Line 1,142: | Line 2,127: | ||
# Replace |
# Replace |
||
$string = preg_replace('/\ba\b/', 'another', $string); |
$string = preg_replace('/\ba\b/', 'another', $string); |
||
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";</ |
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>Ends with 'string' |
<pre>Ends with 'string' |
||
Foud 'a' and replaced it with 'another', resulting in this string: I am another string</pre> |
Foud 'a' and replaced it with 'another', resulting in this string: I am another string</pre> |
||
Line 1,152: | Line 2,137: | ||
PicoLisp doesn't have built-in regex functionality. |
PicoLisp doesn't have built-in regex functionality. |
||
It is easy to call the native C library. |
It is easy to call the native C library. |
||
< |
<syntaxhighlight lang="picolisp">(let (Pat "a[0-9]z" String "a7z") |
||
(use Preg |
(use Preg |
||
(native "@" "regcomp" 'I '(Preg (64 B . 64)) Pat 1) # Compile regex |
(native "@" "regcomp" 'I '(Preg (64 B . 64)) Pat 1) # Compile regex |
||
(when (=0 (native "@" "regexec" 'I (cons NIL (64) Preg) String 0 0 0)) |
(when (=0 (native "@" "regexec" 'I (cons NIL (64) Preg) String 0 0 0)) |
||
(prinl "String \"" String "\" matches regex \"" Pat "\"") ) ) )</ |
(prinl "String \"" String "\" matches regex \"" Pat "\"") ) ) )</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>String "a7z" matches pattern "a[0-9]z"</pre> |
<pre>String "a7z" matches pattern "a[0-9]z"</pre> |
||
===Using Pattern Matching=== |
===Using Pattern Matching=== |
||
Regular expressions are static and inflexible. |
Regular expressions are static and inflexible. |
||
dynamic pattern matching, |
Another possibility is dynamic pattern matching, |
||
where arbitrary conditions can be programmed. |
|||
<lang PicoLisp>(let String "The number <7> is incremented" |
|||
<syntaxhighlight lang="picolisp">(let String "The number <7> is incremented" |
|||
(use (@A @N @Z) |
(use (@A @N @Z) |
||
(and |
(and |
||
(match '(@A "<" @N ">" @Z) (chop String)) |
(match '(@A "<" @N ">" @Z) (chop String)) |
||
(format @N) |
(format @N) |
||
(prinl @A "<" (inc @) ">" @Z) ) ) )</ |
(prinl @A "<" (inc @) ">" @Z) ) ) )</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>The number <8> is incremented</pre> |
<pre>The number <8> is incremented</pre> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">"I am a string" -match '\bstr' # true |
||
"I am a string" -replace 'a\b','no' # I am no string</ |
"I am a string" -replace 'a\b','no' # I am no string</syntaxhighlight> |
||
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. |
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}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="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$ = "<([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, ]*" |
regex_replace$ = "letters[a-z,A-Z,0-9, ]*numbers[a-z,A-Z,0-9, ]*" |
||
Line 1,185: | Line 2,172: | ||
EndIf |
EndIf |
||
Debug ReplaceRegularExpression(2, String$, "char stuff") |
Debug ReplaceRegularExpression(2, String$, "char stuff") |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">import re |
||
string = "This is a string" |
string = "This is a string" |
||
if re.search('string$',string): |
if re.search('string$', string): |
||
print |
print("Ends with string.") |
||
string = re.sub(" a "," another ",string) |
string = re.sub(" a ", " another ", string) |
||
print |
print(string)</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
First, define some strings. |
First, define some strings. |
||
< |
<syntaxhighlight lang="r">pattern <- "string" |
||
text1 <- "this is a matching string" |
text1 <- "this is a matching string" |
||
text2 <- "this does not match"</ |
text2 <- "this does not match"</syntaxhighlight> |
||
Matching with grep. The indices of the texts containing matches are returned. |
Matching with grep. The indices of the texts containing matches are returned. |
||
< |
<syntaxhighlight lang="r">grep(pattern, c(text1, text2)) # 1</syntaxhighlight> |
||
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches. |
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches. |
||
< |
<syntaxhighlight lang="r">regexpr(pattern, c(text1, text2))</syntaxhighlight> |
||
[1] 20 -1 |
[1] 20 -1 |
||
attr(,"match.length") |
attr(,"match.length") |
||
[1] 6 -1 |
[1] 6 -1 |
||
Replacement |
Replacement |
||
< |
<syntaxhighlight lang="r">gsub(pattern, "pair of socks", c(text1, text2))</syntaxhighlight> |
||
[1] "this is a matching pair of socks" "this does not match" |
[1] "this is a matching pair of socks" "this does not match" |
||
=={{header|Racket}}== |
|||
<syntaxhighlight lang="racket"> |
|||
#lang racket |
|||
(define s "I am a string") |
|||
(when (regexp-match? #rx"string$" s) |
|||
(displayln "Ends with 'string'.")) |
|||
(unless (regexp-match? #rx"^You" s) |
|||
(displayln "Does not start with 'You'.")) |
|||
(displayln (regexp-replace " a " s " another ")) |
|||
</syntaxhighlight> |
|||
=={{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}}== |
=={{header|Raven}}== |
||
< |
<syntaxhighlight lang="raven">'i am a string' as str</syntaxhighlight> |
||
Match: |
Match: |
||
< |
<syntaxhighlight lang="raven">str m/string$/ |
||
if "Ends with 'string'\n" print</ |
if "Ends with 'string'\n" print</syntaxhighlight> |
||
Replace: |
Replace once: |
||
<syntaxhighlight lang="raven">str r/ a / another / print</syntaxhighlight> |
|||
<syntaxhighlight lang="raven">str r/ /_/ print</syntaxhighlight> |
|||
Replace all: |
|||
<syntaxhighlight lang="raven">str r/ /_/g print</syntaxhighlight> |
|||
Replace case insensitive: |
|||
<syntaxhighlight lang="raven">str r/ A / another /i print</syntaxhighlight> |
|||
Splitting: |
|||
<lang |
<syntaxhighlight lang="raven">str s/ /</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Regular Expression Matching" |
Title: "Regular Expression Matching" |
||
Author: oofoe |
|||
Date: 2009-12-06 |
|||
URL: http://rosettacode.org/wiki/Regular_expression_matching |
URL: http://rosettacode.org/wiki/Regular_expression_matching |
||
] |
] |
||
Line 1,269: | Line 2,298: | ||
replace string " another " " a " ; Change string back. |
replace string " another " " a " ; Change string back. |
||
print [crlf "Replacement:" string]</ |
print [crlf "Replacement:" string]</syntaxhighlight> |
||
Output: |
|||
{{out}} |
|||
<pre>Subject ends with 'string.' |
<pre>Subject ends with 'string.' |
||
Line 1,280: | Line 2,308: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Rexx does not directly support the use of regular expressions as part of the language. |
Rexx does not directly support the use of regular expressions as part of the language. |
||
<br>However, some rexx interpreters offer support for regular expressions via external function libraries or |
<br>However, some rexx interpreters offer support for regular expressions via external function libraries or |
||
Line 1,286: | Line 2,313: | ||
<br><br>It is also possible to emulate regular expressions through appropriate coding techniques. |
<br><br>It is also possible to emulate regular expressions through appropriate coding techniques. |
||
<br><br>All of the following REXX examples are modeled after the '''PERL''' examples. |
<br><br>All of the following REXX examples are modeled after the '''PERL''' examples. |
||
<lang rexx>/*REXX program demonstrates testing (modeled after Perl example).*/ |
|||
$string = "I am a string" |
|||
say 'The string is:' $string |
|||
x = "string" |
|||
if right($string,length(x))=x then say 'It ends with:' x |
|||
===testing=== |
|||
y = "You" |
|||
<syntaxhighlight lang="rexx">/*REXX program demonstrates testing (modeled after Perl example).*/ |
|||
if left($string,length(y))\=y then say 'It does not start with:' y |
|||
$string="I am a string" |
|||
say 'The string is:' $string |
|||
z = "ring" |
|||
if |
x="string" ; if right($string,length(x))=x then say 'It ends with:' x |
||
y="You" ; if left($string,length(y))\=y then say 'It does not start with:' y |
|||
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 |
z="ring" ; if wordpos(z,$string)==0 then say 'It does not contain the word:' z |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
{{out}} |
|||
'''output''' |
|||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
The string is: I am a string |
The string is: I am a string |
||
It ends with: string |
It ends with: string |
||
Line 1,309: | Line 2,331: | ||
It does not contain the word: ring |
It does not contain the word: ring |
||
</pre> |
</pre> |
||
<lang rexx>/*REXX program demonstrates substitution (modeled after Perl example).*/ |
|||
===substitution (destructive)=== |
|||
<syntaxhighlight lang="rexx">/*REXX program demonstrates substitution (modeled after Perl example).*/ |
|||
$string = "I am a string" |
$string = "I am a string" |
||
old = " a " |
old = " a " |
||
new = " another " |
new = " another " |
||
say 'The original string is:' $string |
say 'The original string is:' $string |
||
say 'old word is:' old |
say 'old word is:' old |
||
say 'new word is:' new |
say 'new word is:' new |
||
$string = changestr(old,$string,new) |
$string = changestr(old,$string,new) |
||
say 'The changed string is:' $string |
say 'The changed string is:' $string |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
{{out}} |
|||
'''output''' |
|||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
The original string is: I am a string |
The original string is: I am a string |
||
old word is: a |
old word is: a |
||
Line 1,326: | Line 2,350: | ||
The changed string is: I am another string |
The changed string is: I am another string |
||
</pre> |
</pre> |
||
<lang rexx>/*REXX program shows non-destructive sub. (modeled after Perl example).*/ |
|||
===substitution (non-destructive)=== |
|||
<syntaxhighlight lang="rexx">/*REXX program shows non-destructive sub. (modeled after Perl example).*/ |
|||
$string = "I am a string" |
$string = "I am a string" |
||
old = " a " |
old = " a " |
||
new = " another " |
new = " another " |
||
say 'The original string is:' $string |
say 'The original string is:' $string |
||
say 'old word is:' old |
say 'old word is:' old |
||
say 'new word is:' new |
say 'new word is:' new |
||
$string2 = changestr(old,$string,new) |
$string2 = changestr(old,$string,new) |
||
say 'The original string is:' $string |
say 'The original string is:' $string |
||
say 'The changed string is:' $string2 |
say 'The changed string is:' $string2 |
||
/*stick a fork in it, we're done.*/</ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
{{out}} |
|||
'''output''' |
|||
<pre> |
|||
<pre style="overflow:scroll"> |
|||
The original string is: I am a string |
The original string is: I am a string |
||
old word is: a |
old word is: a |
||
Line 1,345: | Line 2,372: | ||
The changed string is: I am another string |
The changed string is: I am another string |
||
</pre> |
</pre> |
||
<lang rexx>/*REXX program shows test and substitute (modeled after Perl example).*/ |
|||
===test and substitute=== |
|||
<syntaxhighlight lang="rexx">/*REXX program shows test and substitute (modeled after Perl example).*/ |
|||
$string = "I am a string" |
$string = "I am a string" |
||
old = " am " |
old = " am " |
||
new = " was " |
new = " was " |
||
say 'The original string is:' $string |
say 'The original string is:' $string |
||
say 'old word is:' old |
say 'old word is:' old |
||
say 'new word is:' new |
say 'new word is:' new |
||
if wordpos(old,$string)\==0 then |
if wordpos(old,$string)\==0 then |
||
do |
do |
||
$string = changestr(old,$string,new) |
$string = changestr(old,$string,new) |
||
say 'I was able to find and replace ' old " with " new |
say 'I was able to find and replace ' old " with " new |
||
end |
end |
||
/*stick a fork in it, we're done.*/ |
/*stick a fork in it, we're done.*/</syntaxhighlight> |
||
{{out}} |
|||
</lang> |
|||
<pre> |
|||
'''output''' |
|||
<pre style="overflow:scroll"> |
|||
The original string is: I am a string |
The original string is: I am a string |
||
old word is: am |
old word is: am |
||
new word is: was |
new word is: was |
||
I was able to find and replace am with was |
I was able to find and replace am with was |
||
</pre> |
|||
Some older REXXes don't have a '''changestr''' BIF, so one is included here: ───► [[CHANGESTR.REX]]. |
|||
<br><br> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
# Project : Regular expressions |
|||
text = "I am a text" |
|||
if right(text,4) = "text" |
|||
see "'" + text +"' ends with 'text'" + nl |
|||
ok |
|||
i = substr(text,"am") |
|||
text = left(text,i - 1) + "was" + substr(text,i + 2) |
|||
see "replace 'am' with 'was' = " + text + nl |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
'I am a text' ends with 'text' |
|||
replace 'am' with 'was' = I was a text |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Test |
Test |
||
< |
<syntaxhighlight lang="ruby">str = "I am a string" |
||
p "Ends with 'string'" if str =~ /string$/ |
|||
p "Does not start with 'You'" unless str =~ /^You/</syntaxhighlight> |
|||
Substitute |
Substitute |
||
< |
<syntaxhighlight lang="ruby">str.sub(/ a /, ' another ') #=> "I am another string" |
||
# Or: |
|||
#or |
|||
str[/ a /] = ' another ' #=> "another" |
|||
str #=> "I am another string"</syntaxhighlight> |
|||
puts string</lang> |
|||
Substitute using block |
Substitute using block |
||
< |
<syntaxhighlight lang="ruby">str.gsub(/\bam\b/) { |match| match.upcase } #=> "I AM a string"</syntaxhighlight> |
||
puts "I found #{match}" |
|||
#place "was" instead of the match |
|||
"was" |
|||
end)</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">string$ = "I am a string" |
||
if right$(string$,6) = "string" then print "'";string$;"' ends with 'string'" |
if right$(string$,6) = "string" then print "'";string$;"' ends with 'string'" |
||
i = instr(string$,"am") |
i = instr(string$,"am") |
||
string$ = left$(string$,i - 1) + "was" + mid$(string$,i + 2) |
string$ = left$(string$,i - 1) + "was" + mid$(string$,i + 2) |
||
print "replace 'am' with 'was' = ";string$ |
print "replace 'am' with 'was' = ";string$ |
||
</syntaxhighlight> |
|||
</lang>Output: |
|||
{{out}} |
|||
<pre>'I am a string' ends with 'string' |
<pre>'I am a string' ends with 'string' |
||
replace 'am' with 'was' = I was a string</pre> |
replace 'am' with 'was' = I was a string</pre> |
||
=={{header|Rust}}== |
|||
Note that <code>Regex::new</code> checks for a valid regex and thus returns a <code>Result<Regex, Error></code>. |
|||
<syntaxhighlight lang="rust">use regex::Regex; |
|||
fn main() { |
|||
let s = "I am a string"; |
|||
if Regex::new("string$").unwrap().is_match(s) { |
|||
println!("Ends with string."); |
|||
} |
|||
println!("{}", Regex::new(" a ").unwrap().replace(s, " another ")); |
|||
}</syntaxhighlight> |
|||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
Sather understands POSIX regular expressions. |
Sather understands POSIX regular expressions. |
||
< |
<syntaxhighlight lang="sather">class MAIN is |
||
-- we need to implement the substitution |
-- we need to implement the substitution |
||
regex_subst(re:REGEXP, s, sb:STR):STR is |
regex_subst(re:REGEXP, s, sb:STR):STR is |
||
Line 1,422: | Line 2,480: | ||
#OUT + regex_subst(REGEXP::regexp("am +a +st", true), s, "get the ") + "\n"; |
#OUT + regex_subst(REGEXP::regexp("am +a +st", true), s, "get the ") + "\n"; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
|||
Define |
Define |
||
< |
<syntaxhighlight lang="scala">val Bottles1 = "(\\d+) bottles of beer".r // syntactic sugar |
||
val Bottles2 = """(\d+) bottles of beer""".r // using triple-quotes to preserve backslashes |
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 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</ |
val Bottles4 = new scala.util.matching.Regex("""(\d+) bottles of beer""", "bottles") // with named groups</syntaxhighlight> |
||
Search and replace with string methods: |
Search and replace with string methods: |
||
< |
<syntaxhighlight 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" replace ("99", "98") // Single replacement |
||
"99 bottles of beer" replaceAll ("b", "B") // Multiple replacement</ |
"99 bottles of beer" replaceAll ("b", "B") // Multiple replacement</syntaxhighlight> |
||
Search with regex methods: |
Search with regex methods: |
||
< |
<syntaxhighlight 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 |
"\\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 |
"\\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 findFirstMatchIn "99 bottles of beer" // returns a "Match" object, or None |
||
Bottles4 findPrefixMatchOf "99 bottles of beer" // same thing, for prefixes |
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 |
val bottles = (Bottles4 findFirstMatchIn "99 bottles of beer").get.group("bottles") // Getting a group by name</syntaxhighlight> |
||
val Bottles4(bottles) = "99 bottles of beer" // syntactic sugar (not using group name)</lang> |
|||
Using pattern matching with regex: |
Using pattern matching with regex: |
||
< |
<syntaxhighlight lang="scala">val Some(bottles) = Bottles4 findPrefixOf "99 bottles of beer" // throws an exception if the matching fails; full string must match |
||
for { |
for { |
||
line <- """|99 bottles of beer on the wall |
line <- """|99 bottles of beer on the wall |
||
Line 1,458: | Line 2,516: | ||
for { |
for { |
||
matched <- "(\\w+)".r findAllIn "99 bottles of beer" matchData // matchData converts to an Iterator of Match |
matched <- "(\\w+)".r findAllIn "99 bottles of beer" matchData // matchData converts to an Iterator of Match |
||
} println("Matched from "+matched.start+" to "+matched.end)</ |
} println("Matched from "+matched.start+" to "+matched.end)</syntaxhighlight> |
||
Replacing with regex: |
Replacing with regex: |
||
< |
<syntaxhighlight lang="scala">Bottles2 replaceFirstIn ("99 bottles of beer", "98 bottles of beer") |
||
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")</ |
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")</syntaxhighlight> |
||
=={{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}}== |
=={{header|Shiny}}== |
||
< |
<syntaxhighlight lang="shiny">str: 'I am a string'</syntaxhighlight> |
||
Match text: |
Match text: |
||
< |
<syntaxhighlight lang="shiny">if str.match ~string$~ |
||
say "Ends with 'string'" |
say "Ends with 'string'" |
||
end</ |
end</syntaxhighlight> |
||
Replace text: |
Replace text: |
||
< |
<syntaxhighlight lang="shiny">say str.alter ~ a ~ 'another'</syntaxhighlight> |
||
=={{header|Sidef}}== |
|||
Simple matching: |
|||
<syntaxhighlight lang="ruby">var str = "I am a string"; |
|||
if (str =~ /string$/) { |
|||
print "Ends with 'string'\n"; |
|||
}</syntaxhighlight> |
|||
Global matching: |
|||
<syntaxhighlight lang="ruby">var str = <<'EOF'; |
|||
x:Foo |
|||
y:Bar |
|||
EOF |
|||
while (var m = str=~/(\w+):(\S+)/g) { |
|||
say "#{m[0]} -> #{m[1]}"; |
|||
}</syntaxhighlight> |
|||
Substitutions: |
|||
<syntaxhighlight lang="ruby">var str = "I am a string"; |
|||
# Substitute something mached by a regex |
|||
str.sub!(/ a /, ' another '); # "I am a string" => "I am another string" |
|||
# Remove something matched by a regex |
|||
str -= / \Kanother /i; # "I am another string" => "I am string" |
|||
# Global subtitution with a block |
|||
str = str.gsub(/(\w+)/, {|s1| 'x' * s1.len}); # globaly replace any word with 'xxx' |
|||
say str; # prints: 'x xx xxxxxx'</syntaxhighlight> |
|||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
Line 1,479: | Line 2,611: | ||
This library is still in its early stages. There isn't currently a feature to replace a substring. |
This library is still in its early stages. There isn't currently a feature to replace a substring. |
||
< |
<syntaxhighlight lang="slate"> |
||
'http://slatelanguage.org/test/page?query' =~ '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?'. |
'http://slatelanguage.org/test/page?query' =~ '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?'. |
||
" ==> {'http:'. 'http'. '//slatelanguage.org'. 'slatelanguage.org'. '/test/page'. '?query'. 'query'. Nil} " |
" ==> {'http:'. 'http'. '//slatelanguage.org'. 'slatelanguage.org'. '/test/page'. '?query'. 'query'. Nil} " |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|GNU Smalltalk}} |
|||
<lang smalltalk>|re s s1| |
|||
<syntaxhighlight lang="smalltalk">|re s s1| |
|||
re := Regex fromString: '[a-z]+ing'. |
re := Regex fromString: '[a-z]+ing'. |
||
s := 'this is a matching string'. |
s := 'this is a matching string'. |
||
Line 1,504: | Line 2,637: | ||
]. |
]. |
||
(s replacingRegex: re with: 'modified') displayNl.</ |
(s replacingRegex: re with: 'modified') displayNl.</syntaxhighlight> |
||
{{works with|Pharo}} |
|||
<syntaxhighlight lang="smalltalk"> |
|||
|re s s1| |
|||
re := 'm[a-z]+ing' asRegex. |
|||
s := 'this is a matching string'. |
|||
(re search: s) ifTrue: [ 'matches!' ]. |
|||
s1 := re copy: s replacingMatchesWith: 'modified'. |
|||
</syntaxhighlight> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 1,515: | Line 2,658: | ||
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: |
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: |
||
< |
<syntaxhighlight lang="snobol4">label subject pattern = object :(goto)</syntaxhighlight> |
||
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. |
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 1,521: | Line 2,664: | ||
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): |
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): |
||
< |
<syntaxhighlight lang="snobol4"> string1 = "The SNOBOL4 language is designed for string manipulation." |
||
string1 "SNOBOL4" = "new SPITBOL" :s(changed)f(nochange)</ |
string1 "SNOBOL4" = "new SPITBOL" :s(changed)f(nochange)</syntaxhighlight> |
||
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): |
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): |
||
< |
<syntaxhighlight lang="snobol4"> pi = 3.1415926 |
||
dd = "0123456789" |
dd = "0123456789" |
||
string1 = "For the first circle, the diameter is 2.5 inches." |
string1 = "For the first circle, the diameter is 2.5 inches." |
||
numpat = span(dd) (("." span(dd)) | null) |
numpat = span(dd) (("." span(dd)) | null) |
||
string1 "diameter is " numpat . diam = "circumference is " diam * pi</ |
string1 "diameter is " numpat . diam = "circumference is " diam * pi</syntaxhighlight> |
||
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. |
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 1,538: | Line 2,681: | ||
{{works with|SML/NJ}} |
{{works with|SML/NJ}} |
||
Test |
Test |
||
< |
<syntaxhighlight lang="sml">CM.make "$/regexp-lib.cm"; |
||
structure RE = RegExpFn ( |
structure RE = RegExpFn ( |
||
structure P = AwkSyntax |
structure P = AwkSyntax |
||
Line 1,551: | Line 2,694: | ||
in |
in |
||
print ("matched at position " ^ Int.toString pos ^ "\n") |
print ("matched at position " ^ Int.toString pos ^ "\n") |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Stata}}== |
|||
See '''[http://www.stata.com/help.cgi?regexm regexm]''', '''regexr''' and '''regexs''' in Stata help. |
|||
<syntaxhighlight lang="stata">scalar s="ars longa vita brevis" |
|||
* is there a vowel? |
|||
di regexm(s,"[aeiou]") |
|||
* replace the first vowel with "?" |
|||
di regexr(s,"[aeiou]","?")</syntaxhighlight> |
|||
=={{header|Swift}}== |
|||
===RegularExpressionSearch=== |
|||
Test |
|||
<syntaxhighlight lang="swift">import Foundation |
|||
let str = "I am a string" |
|||
if let range = str.rangeOfString("string$", options: .RegularExpressionSearch) { |
|||
println("Ends with 'string'") |
|||
}</syntaxhighlight> |
|||
Substitute (undocumented) |
|||
<syntaxhighlight lang="swift">import Foundation |
|||
let orig = "I am the original string" |
|||
let result = orig.stringByReplacingOccurrencesOfString("original", withString: "modified", options: .RegularExpressionSearch) |
|||
println(result)</syntaxhighlight> |
|||
===NSRegularExpression=== |
|||
Test |
|||
<syntaxhighlight lang="swift">import Foundation |
|||
if let regex = NSRegularExpression(pattern: "string$", options: nil, error: nil) { |
|||
let str = "I am a string" |
|||
if let result = regex.firstMatchInString(str, options: nil, range: NSRange(location: 0, length: count(str.utf16))) { |
|||
println("Ends with 'string'") |
|||
} |
|||
}</syntaxhighlight> |
|||
Loop through matches |
|||
<syntaxhighlight 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) |
|||
}</syntaxhighlight> |
|||
Substitute |
|||
<syntaxhighlight lang="swift">import Foundation |
|||
let orig = "I am the original string" |
|||
if let regex = NSRegularExpression(pattern: "original", options: nil, error: nil) { |
|||
let result = regex.stringByReplacingMatchesInString(orig, options: nil, range: NSRange(location: 0, length: count(orig.utf16)), withTemplate: "modified") |
|||
println(result) |
|||
}</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Test using <code>regexp</code>: |
Test using <code>regexp</code>: |
||
< |
<syntaxhighlight lang="tcl">set theString "I am a string" |
||
if {[regexp -- {string$} $theString]} { |
if {[regexp -- {string$} $theString]} { |
||
puts "Ends with 'string'" |
puts "Ends with 'string'" |
||
Line 1,562: | Line 2,761: | ||
if {![regexp -- {^You} $theString]} { |
if {![regexp -- {^You} $theString]} { |
||
puts "Does not start with 'You'" |
puts "Does not start with 'You'" |
||
}</ |
}</syntaxhighlight> |
||
Extract substring using <code>regexp</code> |
Extract substring using <code>regexp</code> |
||
< |
<syntaxhighlight lang="tcl">set theString "This string has >123< a number in it" |
||
if {[regexp -- {>(\d+)<} $theString -> number]} { |
if {[regexp -- {>(\d+)<} $theString -> number]} { |
||
puts "Contains the number $number" |
puts "Contains the number $number" |
||
}</ |
}</syntaxhighlight> |
||
Substitute using <code>regsub</code> |
Substitute using <code>regsub</code> |
||
< |
<syntaxhighlight lang="tcl">set theString = "I am a string" |
||
puts [regsub -- { +a +} $theString { another }]</ |
puts [regsub -- { +a +} $theString { another }]</syntaxhighlight> |
||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 1,578: | Line 2,777: | ||
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings. |
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings. |
||
< |
<syntaxhighlight lang="toka">#! Include the regex library |
||
needs regex |
needs regex |
||
Line 1,596: | Line 2,795: | ||
#! try-regex will return a flag. -1 is TRUE, 0 is FALSE |
#! try-regex will return a flag. -1 is TRUE, 0 is FALSE |
||
expression test.1 2 match try-regex . |
expression test.1 2 match try-regex . |
||
expression test.2 2 match try-regex .</ |
expression test.2 2 match try-regex .</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 1,604: | Line 2,803: | ||
Txr is not designed for sed-like filtering, but here is how to do <code>sed -e 's/dog/cat/g'</code>: |
Txr is not designed for sed-like filtering, but here is how to do <code>sed -e 's/dog/cat/g'</code>: |
||
< |
<syntaxhighlight lang="txr">@(collect) |
||
@(coll :gap 0)@mismatch@{match /dog/}@(end)@suffix |
@(coll :gap 0)@mismatch@{match /dog/}@(end)@suffix |
||
@(output) |
@(output) |
||
@(rep)@{mismatch}cat@(end)@suffix |
@(rep)@{mismatch}cat@(end)@suffix |
||
@(end) |
@(end) |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
How it works is that the body of the <code>coll</code> uses a double-variable match: |
How it works is that the body of the <code>coll</code> uses a double-variable match: |
||
an unbound variable followed by a regex-match variable. |
|||
So we collect pairs: pieces of mismatching text, and pieces of text which match the regex <code>dog</code>. At the end, there is usually going to be a piece of text which does not match the body, because it has no match for the regex. Because <code>:gap 0</code> is specified, the coll construct will terminate when faced with this nonmatching text, rather than skipping it in a vain search for a match, which allows <code>@suffix</code> to take on this trailing text. |
|||
The meaning of this combination is, "Search for the regular expression, and if successful, then bind all the characters whcih were skipped over by the search to the first variable, and the matching text to the second variable." |
|||
So we collect pairs: pieces of mismatching text, and pieces of text which match the regex <code>dog</code>. |
|||
At the end, there is usually going to be a piece of text which does not match the body, because it has no match for the regex. |
|||
Because <code>:gap 0</code> is specified, the coll construct will terminate when faced with this nonmatching text, rather than skipping it in a vain search for a match, which allows <code>@suffix</code> to take on this trailing text. |
|||
To output the substitution, we simply spit out the mismatching texts |
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=== |
===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 |
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. |
|||
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. |
|||
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. |
|||
< |
<syntaxhighlight lang="txr">@(freeform) |
||
@(coll :gap 0)@notcomment@{comment /[/][*].%[*][/]/}@(end)@tail |
@(coll :gap 0)@notcomment@{comment /[/][*].%[*][/]/}@(end)@tail |
||
@(output) |
@(output) |
||
@(rep)@notcomment @(end)@tail |
@(rep)@notcomment @(end)@tail |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
===Regexes in TXR Lisp=== |
|||
Parse regex at run time to abstract syntax: |
|||
<syntaxhighlight lang="sh">$ txr -p '(regex-parse "a.*b")' |
|||
(compound #\a (0+ wild) #\b)</syntaxhighlight> |
|||
Dynamically compile regex abstract syntax to regex object: |
|||
<syntaxhighlight lang="sh">$ txr -p "(regex-compile '(compound #\a (0+ wild) #\b))" |
|||
#<sys:regex: 9c746d0></syntaxhighlight> |
|||
Search replace with <code>regsub</code>. |
|||
<syntaxhighlight lang="sh">$ txr -p '(regsub #/a+/ "-" "baaaaaad")' |
|||
"b-d"</syntaxhighlight> |
|||
=={{header|UNIX Shell}}== |
|||
[[bash]] and [[ksh]] implement regular expression matching via the |
|||
<tt>[[</tt> command's <tt>=~</tt> operator. |
|||
[[ksh]] additionally allows regular expression as a flavour of general |
|||
pattern matching. |
|||
===Matching=== |
|||
{{works with|bash}} |
|||
{{works with|ksh}} |
|||
<syntaxhighlight lang="bash">s="I am a string" |
|||
if [[ $s =~ str..g$ ]]; then |
|||
echo "the string ends with 'str..g'" |
|||
fi</syntaxhighlight> |
|||
===Replacing=== |
|||
Given these values |
|||
<syntaxhighlight lang="bash">s="I am the original string" |
|||
re='o.*l' |
|||
repl="modified"</syntaxhighlight> |
|||
{{works with|ksh}} |
|||
Can use regular expressions in parameter expansion |
|||
<syntaxhighlight lang="bash">modified=${s/~(E)$re/$repl} |
|||
echo "$modified" # I am the modified string</syntaxhighlight> |
|||
{{works with|bash}} |
|||
have to break apart the original string to build the modified string. |
|||
<syntaxhighlight lang="bash">if [[ $s =~ $re ]]; then |
|||
submatch=${BASH_REMATCH[0]} |
|||
modified="${s%%$submatch*}$repl${s#*$submatch}" |
|||
echo "$modified" # I am the modified string |
|||
fi</syntaxhighlight> |
|||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala"> |
||
void main(){ |
void main(){ |
||
string sentence = "This is a sample sentence."; |
string sentence = "This is a sample sentence."; |
||
Line 1,642: | Line 2,900: | ||
stdout.printf("Replaced sentence is: %s\n", sentence); |
stdout.printf("Replaced sentence is: %s\n", sentence); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
"s[ai]mple" is in "This is a sample sentence."! |
"s[ai]mple" is in "This is a sample sentence."! |
||
Replaced sentence is: This is a cat sentence. |
Replaced sentence is: This is a cat sentence. |
||
</pre> |
</pre> |
||
=={{header|VBScript}}== |
|||
Replace white spaces with line breaks. |
|||
<syntaxhighlight lang="vb">text = "I need more coffee!!!" |
|||
Set regex = New RegExp |
|||
regex.Global = True |
|||
regex.Pattern = "\s" |
|||
If regex.Test(text) Then |
|||
WScript.StdOut.Write regex.Replace(text,vbCrLf) |
|||
Else |
|||
WScript.StdOut.Write "No matching pattern" |
|||
End If</syntaxhighlight> |
|||
{{in}} |
|||
<pre>I need more coffee!!!</pre> |
|||
{{out}} |
|||
<pre> |
|||
I |
|||
need |
|||
more |
|||
coffee!!! |
|||
</pre> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Vedit can perform searches and matching with either regular expressions, pattern matching codes or plain text. These examples use regular expressions. |
Vedit can perform searches and matching with either regular expressions, pattern matching codes or plain text. These examples use regular expressions. |
||
Match text at cursor location: |
Match text at cursor location: |
||
< |
<syntaxhighlight lang="vedit">if (Match(".* string$", REGEXP)==0) { |
||
Statline_Message("This line ends with 'string'") |
Statline_Message("This line ends with 'string'") |
||
}</ |
}</syntaxhighlight> |
||
Search for a pattern: |
Search for a pattern: |
||
< |
<syntaxhighlight lang="vedit">if (Search("string$", REGEXP+NOERR)) { |
||
Statline_Message("'string' at and of line found") |
Statline_Message("'string' at and of line found") |
||
}</ |
}</syntaxhighlight> |
||
Replace: |
Replace: |
||
< |
<syntaxhighlight lang="vedit">Replace(" a ", " another ", REGEXP+NOERR)</syntaxhighlight> |
||
{{omit from|PARI/GP}} |
|||
=={{header|Web 68}}== |
=={{header|Web 68}}== |
||
< |
<syntaxhighlight lang="web68">@1Introduction. |
||
Web 68 has access to a regular expression module |
Web 68 has access to a regular expression module |
||
which can compile regular expressions, |
|||
use them for matching strings, and replace strings with the matched string. |
|||
use them for matching strings, |
|||
and replace strings with the matched string. |
|||
@a@<Compiler prelude@> |
@a@<Compiler prelude@> |
||
Line 1,722: | Line 3,003: | ||
ca -l mod rosettacoderegex.w68 |
ca -l mod rosettacoderegex.w68 |
||
That's it. The resulting binary will print |
That's it. The resulting binary will print |
||
'String "string$" matches string "This is a string"'</ |
'String "string$" matches string "This is a string"'</syntaxhighlight> |
||
[[Category:Regular expressions]] |
|||
=={{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. |
|||
<syntaxhighlight lang="zkl">var re=RegExp(".*string$"); |
|||
re.matches("I am a string") //-->True |
|||
var s="I am a string thing" |
|||
re=RegExp("(string)") // () means group, ie if you see it, save it |
|||
re.search(s,True) //-->True, .search(x,True) means search for a match, ie don't need .* |
|||
p,n:=re.matched[0] //.matched-->L(L(7,6),"string") |
|||
String(s[0,p],"FOO",s[p+n,*]) //-->"I am a FOO thing" |
|||
re.search(s,True); // using .matched clears it |
|||
m:=re.matched[1]; |
|||
s.replace(m,"FOO"); // -->"I am a FOO thing"</syntaxhighlight> |
|||
Using a mutable byte bucket: |
|||
<syntaxhighlight 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"</syntaxhighlight> |
Latest revision as of 18:36, 16 June 2024
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
-
- match a string against a regular expression
- substitute part of a string using a regular expression
11l
V string = ‘This is a string’
I re:‘string$’.search(string)
print(‘Ends with string.’)
string = string.replace(re:‘ a ’, ‘ another ’)
print(string)
- Output:
Ends with string. This is another string
8th
"haystack" /a./ r:match . cr
"haystack" /a./ "blah" s:replace! . cr
- Output:
1 hblahstblahk
ABAP
DATA: text TYPE string VALUE 'This is a Test'.
FIND FIRST OCCURRENCE OF REGEX 'is' IN text.
IF sy-subrc = 0.
cl_demo_output=>write( 'Regex matched' ).
ENDIF.
REPLACE ALL OCCURRENCES OF REGEX '[t|T]est' IN text WITH 'Regex'.
cl_demo_output=>write( text ).
cl_demo_output=>display( ).
Output:
Regex matched This is a Regex
Ada
There is no Regular Expression library in the Ada Standard, so I am using one of the libraries provided by gnat/gcc.
with Ada.Text_IO; with Gnat.Regpat; use Ada.Text_IO;
procedure Regex is
package Pat renames Gnat.Regpat;
procedure Search_For_Pattern(Compiled_Expression: Pat.Pattern_Matcher;
Search_In: String;
First, Last: out Positive;
Found: out Boolean) is
Result: Pat.Match_Array (0 .. 1);
begin
Pat.Match(Compiled_Expression, Search_In, Result);
Found := not Pat."="(Result(1), Pat.No_Match);
if Found then
First := Result(1).First;
Last := Result(1).Last;
end if;
end Search_For_Pattern;
Word_Pattern: constant String := "([a-zA-Z]+)";
Str: String:= "I love PATTERN matching!";
Current_First: Positive := Str'First;
First, Last: Positive;
Found: Boolean;
begin
-- first, find all the words in Str
loop
Search_For_Pattern(Pat.Compile(Word_Pattern),
Str(Current_First .. Str'Last),
First, Last, Found);
exit when not Found;
Put_Line("<" & Str(First .. Last) & ">");
Current_First := Last+1;
end loop;
-- second, replace "PATTERN" in Str by "pattern"
Search_For_Pattern(Pat.Compile("(PATTERN)"), Str, First, Last, Found);
Str := Str(Str'First .. First-1) & "pattern" & Str(Last+1 .. Str'Last);
Put_Line(Str);
end Regex;
- Output:
<I> <love> <PATTERN> <matching> I love pattern matching!
ALGOL 68
The routines grep in strings and sub in string are not part of ALGOL 68's standard prelude.
INT match=0, no match=1, out of memory error=2, other error=3;
STRING str := "i am a string";
# Match: #
STRING m := "string$";
INT start, end;
IF grep in string(m, str, start, end) = match THEN printf(($"Ends with """g""""l$, str[start:end])) FI;
# Replace: #
IF sub in string(" a ", " another ",str) = match THEN printf(($gl$, str)) FI;
- Output:
Ends with "string" i am another string
Standard ALGOL 68 does have an primordial form of pattern matching called a format. This is designed to extract values from input data. But it can also be used for outputting (and transputting) the original data.
For example:
FORMAT pattern = $ddd" "c("cats","dogs")$;
FILE file; STRING book; associate(file, book);
on value error(file, (REF FILE f)BOOL: stop);
on format error(file, (REF FILE f)BOOL: stop);
book := "100 dogs";
STRUCT(INT count, type) dalmatians;
getf(file, (pattern, dalmatians));
print(("Dalmatians: ", dalmatians, new line));
count OF dalmatians +:=1;
printf(($"Gives: "$, pattern, dalmatians, $l$))
- Output:
Dalmatians: +100 +2 Gives 101 dogs
Amazing Hopper
task 1: match a string against a regular expression (Hopper use POSIX):
#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)
- Output:
11 18 SN12 7NY
Task 2: Hopper does not substitute using regular expressions, but using proper functions.
#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)
- Output:
We are at another thing for this course We are at another thing for this course
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
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 Late Night Software, the developer of Script Debugger, has released a 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 do shell script 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:
-- 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
ASObjC uses ICU regex:
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
As an alternative to the NSString regex options used above, there's also a dedicated NSRegularExpression class:
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
- Output:
The latter three scripts all return:
"string I am the modified string"
Shane Stanley has released a script library called RegexAndStuffLib, which is written in ASObjC, but provides less verbose commands for use in client scripts.
Argile
use std, regex
(: matching :)
if "some matchable string" =~ /^some" "+[a-z]*" "+string$/
echo string matches
else
echo string "doesn't" match
(: replacing :)
let t = strdup "some allocated string"
t =~ s/a/"4"/g
t =~ s/e/"3"/g
t =~ s/i/"1"/g
t =~ s/o/"0"/g
t =~ s/s/$/g
print t
free t
(: flushing regex allocations :)
uninit regex
check mem leak; use dbg (:optional:)
(note that it needs to be compiled with argrt library)
- Output:
string matches $0m3 4ll0c4t3d $tr1ng
Arturo
s: "This is a string"
if contains? s {/string$/} -> print "yes, it ends with 'string'"
replace 's {/[as]/} "x"
print s
- Output:
yes, it ends with 'string' Thix ix x xtring
AutoHotkey
MsgBox % foundpos := RegExMatch("Hello World", "World$")
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")
AWK
AWK supports regular expressions, which are typically enclosed using slash symbols at the front and back, and the tilde regular expression binding operator:
$ awk '{if($0~/[A-Z]/)print "uppercase detected"}'
abc
ABC
uppercase detected
As shorthand, a regular expression in the condition part fires if it matches an input line:
awk '/[A-Z]/{print "uppercase detected"}'
def
DeF
uppercase detected
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):
$ awk '{gsub(/[A-Z]/,"*");print}'
abCDefG
ab**ef*
$ awk '{gsub(/[A-Z]/,"(&)");print}'
abCDefGH
ab(C)(D)ef(G)(H)
This variant matches one or more uppercase letters in one round:
$ awk '{gsub(/[A-Z]+/,"(&)");print}'
abCDefGH
ab(CD)ef(GH)
Regular expression negation can be achieved by combining the regular expression binding operator with a logical not operator, as follows:
if (text !~ /strawberry/) {
print "Match not found"
}
BBC BASIC
Uses the gnu_regex library.
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
SYS "GetProcAddress", gnu_regex%, "regexec" TO regexec
DIM regmatch{start%, finish%}, buffer% 256
REM Find all 'words' in a string:
teststr$ = "I love PATTERN matching!"
pattern$ = "([a-zA-Z]+)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
first% = 1
REPEAT
SYS regexec, buffer%, MID$(teststr$, first%), 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
PRINT "<" MID$(teststr$, first%+s%, f%-s%) ">"
first% += f%
ENDIF
UNTIL result%
REM Replace 'PATTERN' with 'pattern':
teststr$ = "I love PATTERN matching!"
pattern$ = "(PATTERN)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
SYS regexec, buffer%, teststr$, 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
MID$(teststr$, s%+1, f%-s%) = "pattern"
PRINT teststr$
ENDIF
SYS "FreeLibrary", gnu_regex%
- Output:
<I> <love> <PATTERN> <matching> I love pattern matching!
Bracmat
Pattern matching in Bracmat is inspired by pattern matching in Snobol. It also is quite different from regular expressions:
- Patterns in Bracmat are not greedy
- It is not possible to replace substrings, because values can never be changed
- Patterns always must match all of the subject
- Strings as well as complex data can be subjected to pattern matching
List all rational numbers smaller then 7 hidden in the string "fgsakg789/35768685432fkgha"
@("fesylk789/35768poq2art":? (#<7:?n & out$!n & ~) ?)
- Output:
789/357 789/3576 789/35768 89/35 89/357 89/3576 89/35768 9/3 9/35 9/357 9/3576 9/35768 3 5 6 2
After the last number, the match expression fails.
Brat
Test
str = "I am a string"
true? str.match(/string$/)
{ p "Ends with 'string'" }
false? str.match(/^You/)
{ p "Does not start with 'You'" }
Substitute
# Substitute in copy
str2 = str.sub(/ a /, " another ")
p str # original unchanged
p str2 # prints "I am another string"
# Substitute in place
str.sub!(/ a /, " another ")
p str # prints "I am another string"
# Substitute with a block
str.sub! /a/
{ match | match.upcase }
p str # prints "I Am Another string"
C
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.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h>
int main()
{
regex_t preg;
regmatch_t substmatch[1];
const char *tp = "string$";
const char *t1 = "this is a matching string";
const char *t2 = "this is not a matching string!";
const char *ss = "istyfied";
regcomp(&preg, "string$", REG_EXTENDED);
printf("'%s' %smatched with '%s'\n", t1,
(regexec(&preg, t1, 0, NULL, 0)==0) ? "" : "did not ", tp);
printf("'%s' %smatched with '%s'\n", t2,
(regexec(&preg, t2, 0, NULL, 0)==0) ? "" : "did not ", tp);
regfree(&preg);
/* change "a[a-z]+" into "istifyed"?*/
regcomp(&preg, "a[a-z]+", REG_EXTENDED);
if ( regexec(&preg, t1, 1, substmatch, 0) == 0 )
{
//fprintf(stderr, "%d, %d\n", substmatch[0].rm_so, substmatch[0].rm_eo);
char *ns = malloc(substmatch[0].rm_so + 1 + strlen(ss) +
(strlen(t1) - substmatch[0].rm_eo) + 2);
memcpy(ns, t1, substmatch[0].rm_so+1);
memcpy(&ns[substmatch[0].rm_so], ss, strlen(ss));
memcpy(&ns[substmatch[0].rm_so+strlen(ss)], &t1[substmatch[0].rm_eo],
strlen(&t1[substmatch[0].rm_eo]));
ns[ substmatch[0].rm_so + strlen(ss) +
strlen(&t1[substmatch[0].rm_eo]) ] = 0;
printf("mod string: '%s'\n", ns);
free(ns);
} else {
printf("the string '%s' is the same: no matching!\n", t1);
}
regfree(&preg);
return 0;
}
Alternative using GLib
The task is a bit easier with GLib's Perl-compatible regular expression functionality.
#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;
}
- Output:
Does the string match the pattern '^[a-z]+$'? string = 'test': yes string = 'Test': no Replace pattern '[0-9]' in string 'Test2' by 'X': 'TestX'
C#
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);
}
}
C++
Standards earlier than C++11 can make use of Boost's Regex library via boost/regex.hpp
#include <iostream>
#include <string>
#include <iterator>
#include <regex>
int main()
{
std::regex re(".* string$");
std::string s = "Hi, I am a string";
// match the complete string
if (std::regex_match(s, re))
std::cout << "The string matches.\n";
else
std::cout << "Oops - not found?\n";
// match a substring
std::regex re2(" a.*a");
std::smatch match;
if (std::regex_search(s, match, re2))
{
std::cout << "Matched " << match.length()
<< " characters starting at " << match.position() << ".\n";
std::cout << "Matched character sequence: \""
<< match.str() << "\"\n";
}
else
{
std::cout << "Oops - not found?\n";
}
// replace a substring
std::string dest_string;
std::regex_replace(std::back_inserter(dest_string),
s.begin(), s.end(),
re2,
"'m now a changed");
std::cout << dest_string << std::endl;
}
Clojure
(let [s "I am a string"]
;; match
(when (re-find #"string$" s)
(println "Ends with 'string'."))
(when-not (re-find #"^You" s)
(println "Does not start with 'You'."))
;; substitute
(println (clojure.string/replace s " a " " another "))
)
Common Lisp
Uses CL-PPCRE - Portable Perl-compatible regular expressions for Common 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'")))
Substitute
(let* ((string "I am a string")
(string (cl-ppcre:regex-replace " a " string " another ")))
(write-line string))
Test and Substitute
(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'."))))
CLISP regexp engine
Clisp comes with built-in regexp matcher. On a Clisp prompt:
[1]> (regexp:match "fox" "quick fox jumps")
#S(REGEXP:MATCH :START 6 :END 9)
To find all matches, loop with different :start
keyword.
Replacing text can be done with the help of REGEXP:REGEXP-SPLIT
function:
[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"
D
void main() {
import std.stdio, std.regex;
immutable s = "I am a string";
// Test.
if (s.match("string$"))
"Ends with 'string'.".writeln;
// Substitute.
s.replace(" a ".regex, " another ").writeln;
}
- Output:
Ends with 'string'. I am another string
In std.string there are string functions to perform the same operations more efficiently.
Dart
RegExp regexp = new RegExp(r'\w+\!');
String capitalize(Match m) => '${m[0].substring(0, m[0].length-1).toUpperCase()}';
void main(){
String hello = 'hello hello! world world!';
String hellomodified = hello.replaceAllMapped(regexp, capitalize);
print(hello);
print(hellomodified);
}
- Output:
hello hello! world world! hello HELLO world WORLD
Delphi
Sample program that uses a regex, for translate a line of code in cpp to pascal.
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.
- Output:
Expression: if ( a < 0 ) { return -a; } Is a single If in Cpp: Translate to Pascal: If a < 0 then result:= -a;
Elixir
Elixir allows pattern matching using the ~r
sigil.
str = "This is a string"
if str =~ ~r/string$/, do: IO.inspect "str ends with 'string'"
A number of modifiers can be appended to the regular expression; ~r/pattern/i
, for instance, toggles case insensitivity.
str =~ ~r/this/ # => false
str =~ ~r/this/i # => true
Both Regex
and String
have a replace
function.
str1 = ~r/a/ |> Regex.replace(str,"another")
str2 = str1 |> String.replace(~r/another/,"even another")
Regex.replace
allows for a function to be used as a replacement value. A function can modify the found pattern.
str3 = ~r/another/ |> Regex.replace(str2, fn x -> "#{String.upcase(x)}" end)
- Output:
str ends with 'string'
false
true
"This is another string"
"This is even another string"
"This is even ANOTHER string"
Emacs Lisp
(let ((string "I am a string"))
(when (string-match-p "string$" string)
(message "Ends with 'string'"))
(message "%s" (replace-regexp-in-string " a " " another " string)))
- Output:
Ends with 'string' I am another string
Erlang
match() ->
String = "This is a string",
case re:run(String, "string$") of
{match,_} -> io:format("Ends with 'string'~n");
_ -> ok
end.
substitute() ->
String = "This is a string",
NewString = re:replace(String, " a ", " another ", [{return, list}]),
io:format("~s~n",[NewString]).
F#
open System
open System.Text.RegularExpressions
[<EntryPoint>]
let main argv =
let str = "I am a string"
if Regex("string$").IsMatch(str) then Console.WriteLine("Ends with string.")
let rstr = Regex(" a ").Replace(str, " another ")
Console.WriteLine(rstr)
0
Factor
USING: io kernel prettyprint regexp ;
IN: rosetta-code.regexp
"1000000" R/ 10+/ matches? . ! Does the entire string match the regexp?
"1001" R/ 10+/ matches? .
"1001" R/ 10+/ re-contains? . ! Does the string contain the regexp anywhere?
"blueberry pie" R/ \p{alpha}+berry/ "pumpkin" re-replace print
- Output:
t f t pumpkin pie
Forth
Test/Match
include ffl/rgx.fs
\ Create a regular expression variable 'exp' in the dictionary
rgx-create exp
\ Compile an expression
s" Hello (World)" exp rgx-compile [IF]
.( Regular expression successful compiled.) cr
[THEN]
\ (Case sensitive) match a string with the expression
s" Hello World" exp rgx-cmatch? [IF]
.( String matches with the expression.) cr
[ELSE]
.( No match.) cr
[THEN]
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
- Output:
'I am a text' ends with 'text' replace 'am' with 'was' = I was a text
Frink
Pattern matching:
line = "My name is Inigo Montoya."
for [first, last] = line =~ %r/my name is (\w+) (\w+)/ig
{
println["First name is: $first"]
println["Last name is: $last"]
}
Replacement: (Replaces in the variable line
)
line =~ %s/Frank/Frink/g
Gambas
Click this link to run this code
Public Sub Main()
Dim sString As String = "Hello world!"
If sString Ends "!" Then Print sString & " ends with !"
If sString Begins "Hel" Then Print sString & " begins with 'Hel'"
sString = Replace(sString, "world", "moon")
Print sString
End
Output:
Hello world! ends with ! Hello world! begins with 'Hel' Hello moon!
GeneXus
Interesting link: http://wiki.gxtechnical.com/commwiki/servlet/hwiki?Regular+Expressions+%28RegEx%29,
Replacement:
&string = &string.ReplaceRegEx("^\s+|\s+$", "") // it's a trim!
&string = &string.ReplaceRegEx("Another (Match)", "Replacing $1") // Using replace groups
Check match:
If (&string.IsMatch("regex$"))
// The string ends with "regex"
EndIf
Split RegEx:
&stringCollection = &string.SplitRegEx("^\d{2,4}")
Matches:
&RegExMatchCollection = &string.Matches("(pa)tt(ern)")
For &RegExMatch In &RegExMatchCollection
&FullMatch = &RegExMatch.Value // &FullMatch contains the full pattern match: "pattern"
For &matchVarchar In &RegExMatch.Groups
// &matchVarchar contains group matches: "pa", "ern"
EndFor
EndFor
Flags:
s - Dot matches all (including newline)
m - multiline
i - ignore case
Using Flags Sintax: (?flags)pattern
Example:
&string = &string.ReplaceRegEx("(?si)IgnoreCase.+$", "") // Flags s and i
Error Handling:
&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()
Genie
[indent=4]
/* Regular expressions, in Genie */
init
var sentence = "This is a sample sentence."
try
var re = new Regex("s[ai]mple")
if re.match(sentence)
print "matched '%s' in '%s'", re.get_pattern(), sentence
var offs = 0
print("replace with 'different': %s",
re.replace(sentence, sentence.length, offs, "different"))
except err:RegexError
print err.message
- Output:
prompt$ valac regularExpressions.gs prompt$ ./regularExpressions matched 's[ai]mple' in 'This is a sample sentence.' replace with 'different': This is a different sentence.
Go
package main
import "fmt"
import "regexp"
func main() {
str := "I am the original string"
// Test
matched, _ := regexp.MatchString(".*string$", str)
if matched { fmt.Println("ends with 'string'") }
// Substitute
pattern := regexp.MustCompile("original")
result := pattern.ReplaceAllString(str, "modified")
fmt.Println(result)
}
Groovy
"Matching" Solution (it's complicated):
import java.util.regex.*;
def woodchuck = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
def pepper = "Peter Piper picked a peck of pickled peppers"
println "=== Regular-expression String syntax (/string/) ==="
def woodRE = /[Ww]o\w+d/
def piperRE = /[Pp]\w+r/
assert woodRE instanceof String && piperRE instanceof String
assert (/[Ww]o\w+d/ == "[Ww]o\\w+d") && (/[Pp]\w+r/ == "[Pp]\\w+r")
println ([woodRE: woodRE, piperRE: piperRE])
println ()
println "=== Pattern (~) operator ==="
def woodPat = ~/[Ww]o\w+d/
def piperPat = ~piperRE
assert woodPat instanceof Pattern && piperPat instanceof Pattern
def woodList = woodchuck.split().grep(woodPat)
println ([exactTokenMatches: woodList])
println ([exactTokenMatches: pepper.split().grep(piperPat)])
println ()
println "=== Matcher (=~) operator ==="
def wwMatcher = (woodchuck =~ woodRE)
def ppMatcher = (pepper =~ /[Pp]\w+r/)
def wpMatcher = (woodchuck =~ /[Pp]\w+r/)
assert wwMatcher instanceof Matcher && ppMatcher instanceof Matcher
assert wwMatcher.toString() == woodPat.matcher(woodchuck).toString()
assert ppMatcher.toString() == piperPat.matcher(pepper).toString()
assert wpMatcher.toString() == piperPat.matcher(woodchuck).toString()
println ([ substringMatches: wwMatcher.collect { it }])
println ([ substringMatches: ppMatcher.collect { it }])
println ([ substringMatches: wpMatcher.collect { it }])
println ()
println "=== Exact Match (==~) operator ==="
def containsWoodRE = /.*/ + woodRE + /.*/
def containsPiperRE = /.*/ + piperRE + /.*/
def wwMatches = (woodchuck ==~ containsWoodRE)
assert wwMatches instanceof Boolean
def wwNotMatches = ! (woodchuck ==~ woodRE)
def ppMatches = (pepper ==~ containsPiperRE)
def pwNotMatches = ! (pepper ==~ containsWoodRE)
def wpNotMatches = ! (woodchuck ==~ containsPiperRE)
assert wwMatches && wwNotMatches && ppMatches && pwNotMatches && pwNotMatches
println ("'${woodchuck}' ${wwNotMatches ? 'does not' : 'does'} match '${woodRE}' exactly")
println ("'${woodchuck}' ${wwMatches ? 'does' : 'does not'} match '${containsWoodRE}' exactly")
- Output:
=== Regular-expression String syntax (/string/)=== [woodRE:[Ww]o\w+d, piperRE:[Pp]\w+r] === Pattern (~) operator === [exactTokenMatches:[wood, would]] [exactTokenMatches:[Peter, Piper]] === Matcher (=~) operator === [substringMatches:[wood, would, wood, wood, wood]] [substringMatches:[Peter, Piper, pepper]] [substringMatches:[]] === Exact Match (==~) operator === 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?' does not match '[Ww]o\w+d' exactly 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?' does match '.*[Ww]o\w+d.*' exactly
Replacement Solution (String.replaceAll()):
println woodchuck.replaceAll(/c\w+k/, "CHUCK")
- Output:
How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood?
Reusable Replacement Solution (Matcher.replaceAll()):
def ck = (woodchuck =~ /c\w+k/)
println (ck.replaceAll("CHUCK"))
println (ck.replaceAll("wind"))
println (ck.replaceAll("pile"))
println (ck.replaceAll("craft"))
println (ck.replaceAll("block"))
println (ck.replaceAll("row"))
println (ck.replaceAll("shed"))
println (ck.replaceAll("man"))
println (ck.replaceAll("work"))
println (ck.replaceAll("pickle"))
- Output:
How much wood would a woodCHUCK CHUCK if a woodCHUCK could CHUCK wood? How much wood would a woodwind wind if a woodwind could wind wood? How much wood would a woodpile pile if a woodpile could pile wood? How much wood would a woodcraft craft if a woodcraft could craft wood? How much wood would a woodblock block if a woodblock could block wood? How much wood would a woodrow row if a woodrow could row wood? How much wood would a woodshed shed if a woodshed could shed wood? How much wood would a woodman man if a woodman could man wood? How much wood would a woodwork work if a woodwork could work wood? How much wood would a woodpickle pickle if a woodpickle could pickle wood?
Haskell
Test
import Text.Regex
str = "I am a string"
case matchRegex (mkRegex ".*string$") str of
Just _ -> putStrLn $ "ends with 'string'"
Nothing -> return ()
Substitute
import Text.Regex
orig = "I am the original string"
result = subRegex (mkRegex "original") orig "modified"
putStrLn $ result
HicEst
CHARACTER string*100/ "The quick brown fox jumps over the lazy dog" /
REAL, PARAMETER :: Regex=128, Count=256
characters_a_m = INDEX(string, "[a-m]", Regex+Count) ! counts 16
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
Icon and Unicon
Regex includes procedures to provide access to regular expressions within native string scanning and matching expressions. 'ReFind' and 'ReMatch' respectively generate the sequence of beginning and ending positions matched by a regular expression. Additionally, there is a regular expression pattern compiler 'RePat' and other supporting functions and variables.
- Output:
"A simple string" matches "string$" "A simple replacement"
Inform 7
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.
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";
J
J's regex support is built on top of PCRE.
load'regex' NB. Load regex library
str =: 'I am a string' NB. String used in examples.
Matching:
'.*string$' rxeq str NB. 1 is true, 0 is false
1
Substitution:
('am';'am still') rxrplc str
I am still a string
Note: use
open'regex'
to read the source code for the library. The comments list 6 main definitions and a dozen utility definitions.
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(" +", " ");
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);
}
/* split a string using a pattern */
String[] strings = "abc\r\ndef\r\nghi".split("\r\n?");
An alternate demonstration
Test
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'");
}
To match part of a string, or to process matches:
import java.util.regex.*;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher(str);
while (m.find()) {
// use m.group() to extract matches
}
Substitute
String orig = "I am the original string";
String result = orig.replaceAll("original", "modified");
// result is now "I am the modified string"
JavaScript
Test/Match
var subject = "Hello world!";
// Two different ways to create the RegExp object
// Both examples use the exact same pattern... matching "hello "
var re_PatternToMatch = /Hello (World)/i; // creates a RegExp literal with case-insensitivity
var re_PatternToMatch2 = new RegExp("Hello (World)", "i");
// Test for a match - return a bool
var isMatch = re_PatternToMatch.test(subject);
// Get the match details
// Returns an array with the match's details
// matches[0] == "Hello world"
// matches[1] == "world"
var matches = re_PatternToMatch2.exec(subject);
Substitute
var subject = "Hello world!";
// Perform a string replacement
// newSubject == "Replaced!"
var newSubject = subject.replace(re_PatternToMatch, "Replaced");
jq
Recent versions of jq (jq > 1.4) include PCRE regex support using the Oniguruma library.
Test:
"I am a string" | test("string$")
yields: true
Substitutution:
"I am a string" | sub(" a "; " another ")
yields: "I am another string"
Substitution using capture:
"abc" | sub( "(?<head>^.)(?<tail>.*)"; "\(.head)-\(.tail)")
yields: "a-bc"
Jsish
/* Regular expressions, in Jsish */
var re = /s[ai]mple/;
var sentence = 'This is a sample sentence';
var matches = sentence.match(re);
if (matches.length > 0) printf('%s found in "%s" using %q\n', matches[0], sentence, re);
var replaced = sentence.replace(re, "different");
printf("replaced sentence is: %s\n", replaced);
- Output:
prompt$ jsish regularExpressions.jsi sample found in "This is a sample sentence" using "/s[ai]mple/" replaced sentence is: This is a different sentence
Julia
Julia implements Perl-compatible regular expressions (via the built-in PCRE library). To test for a match:
s = "I am a string"
if ismatch(r"string$", s)
println("'$s' ends with 'string'")
end
To perform replacements:
s = "I am a string"
s = replace(s, r" (a|an) ", " another ")
There are many other features of Julia's regular-expression support, too numerous to list here.
Kotlin
// version 1.0.6
fun main(args: Array<String>) {
val s1 = "I am the original string"
val r1 = Regex("^.*string$")
if (s1.matches(r1)) println("`$s1` matches `$r1`")
val r2 = Regex("original")
val s3 = "replacement"
val s2 = s1.replace(r2, s3)
if (s2 != s1) println("`$s2` replaces `$r2` with `$s3`")
}
- Output:
`I am the original string` matches `^.*string$` `I am the replacement string` replaces `original` with `replacement`
langur
The following examples use re2 regex literals.
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 (->).
To match a string, ...
if "somestring" -> re/abc/ { ... }
Or...
if x, y = submatch(re/(abc+).+?(def)/, "somestring") { ... }
Or...
switch "somestring" {
case -> re/abc/: ...
...
}
Or...
switch -> re/abc/ {
case "somestring": ...
...
}
Substitution does not alter the original string.
replace("abcdef", re/abc/, "Y")
# result: "Ydef"
Lasso
Lasso has built in support for regular expressions using ICU regexps.
local(mytext = 'My name is: Stone, Rosetta
My name is: Hippo, Campus
')
local(regexp = regexp(
-find = `(?m)^My name is: (.*?), (.*?)$`,
-input = #mytext,
-replace = `Hello! I am $2 $1`,
-ignorecase
))
while(#regexp -> find) => {^
#regexp -> groupcount > 1 ? (#regexp -> matchString(2) -> trim&) + '<br />'
^}
#regexp -> reset(-input = #mytext)
#regexp -> findall
#regexp -> reset(-input = #mytext)
'<br />'
#regexp -> replaceall
Rosetta Campus array(My name is: Stone, Rosetta, My name is: Hippo, Campus) Hello! I am Rosetta Stone Hello! I am Campus Hippo
Lua
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 (%
) is generally used instead of a backslash to start a character class or a reference for a match in a substitution.
test = "My name is Lua."
pattern = ".*name is (%a*).*"
if test:match(pattern) then
print("Name found.")
end
sub, num_matches = test:gsub(pattern, "Hello, %1!")
print(sub)
M2000 Interpreter
We can use COM objects so we can use VBscript.RegExp
Properties Count and List() are bound with MyMatches using smart pointer. So MyMatches can change object, and Count and List() can operate with new object.
Com objects always have one real pointer. We can't returned id (because at the exit of module or function where we create it pointer get Nothing, so properties have a "broken" pointer, and return of object returns a broken smart pointer). We can pass by reference com objects, or we can use it as Globals (until "mother" module finish). We can use WithEvent if we wish to get events. We have to make functions with object_event name (note that forms event use dot not underscore).
We can use named parameters (for Word) Try {Method Documents, "add", "", DocumentType:=WdNewWebPage as doc1}
New version using enumerator from object. Including a help SUB for displaying all functions of a COM object. Enumerators for some COM objects are number of function -4&, we can use this in place of string for the name of property.
Module CheckIt {
declare global ObjRegEx "VBscript.RegExp"
Function RegEx.Replace$(from$, what$) {
Method ObjRegEx, "Replace", from$, what$ as response$
=response$
}
Function RegEx.Test(what$) {
Method ObjRegEx, "Test", what$ as response
=response
}
Print Type$(ObjRegEx)
With ObjRegEx, "Global", True, "Pattern" as pattern$
pattern$="Mona Lisa"
Print RegEx.Test("The Mona Lisa is in the Louvre.")=true
Print RegEx.Replace$("The Mona Lisa is in the Louvre.", "La Gioconda")
Pattern$ = " {2,}"
Print "Myer Ken, Vice President, Sales and Services"
\\ Removing some spaces
Print RegEx.Replace$("Myer Ken, Vice President, Sales and Services", " ")
pattern$="(\d{3})-(\d{3})-(\d{4})"
Method ObjRegEx, "Execute", "555-123-4567, 555-943-6717" as MyMatches
Print Type$(MyMatches) ' it is a IMatchCollection2
With MyMatches, "Count" as count, "Item" as List$()
For i=0 to Count-1 : Print List$(i) : Next i
Print RegEx.Replace$("555-123-4567, 555-943-6717", "($1) $2-$3")
Pattern$ = "(\S+), (\S+)"
Print RegEx.Replace$("Myer, Ken", "$2 $1")
Method ObjRegEx, "Execute", "Myer, Ken" as MyMatches
Rem : DisplayFunctions(MyMatches)
\\ we can use Enumerator
With MyMatches, "_NewEnum" as New Matches
Rem : DisplayFunctions(Matches)
With Matches, "Value" as New item$
While Matches {
Print Item$
}
\\ Or just using the list$()
For i=0 to Count-1 : Print List$(i) : Next i
declare ObjRegEx Nothing
End
Sub DisplayFunctions(x)
Local cc=param(x), ec=each(cc)
while ec {
Print eval$(ec) ' print every function/property of object x
}
End Sub
}
Checkit
\\ internal has no pattern. There is a like operator (~) for strings which use pattern matching (using VB6 like). We can use Instr() and RInstr() for strings.
Module Internal {
what$="Mona Lisa"
Document a$="The Mona Lisa is in the Louvre."
Find a$, what$
Read FindWhere
If FindWhere<>0 then Read parNo, parlocation
\\ replace in place
Insert FindWhere, Len(what$) a$="La Gioconda"
Report a$
n$="The Mona Lisa is in the Louvre, not the Mona Lisa"
Report Replace$("Mona Lisa", "La Gioconda", n$, 1, 1) ' replace from start only one
dim a$()
a$()=Piece$("Myer, Ken",", ")
Print a$(1)+", "+a$(0)="Ken, Myer"
}
Internal
M4
regexp(`GNUs not Unix', `\<[a-z]\w+')
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')
- Output:
5 a not b ot c
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");
- Output:
true false "abcd-bc-d" "Maple is Canadian"
Mathematica/Wolfram Language
StringCases["I am a string with the number 18374 in me",RegularExpression["[0-9]+"]]
StringReplace["I am a string",RegularExpression["I\\sam"] -> "I'm"]
The in-notebook output, in order:
{18374} I'm a string
MAXScript
samples = #("Some string 123","Example text 123","string",\
"ThisString Will Not Match","A123,333,string","123451")
samples2 = #("I am a string","Me too.")
regex = dotnetobject "System.Text.RegularExpressions.Regex" ".*\bstring*"
regex2 = dotnetobject "System.Text.RegularExpressions.Regex" "\ba\b"
clearlistener()
format "Pattern is : %\n" (regex.toString())
for i in samples do
(
if regex.ismatch(i) then
(
format "The string \"%\" matches the pattern\n" i
)
else
(
format "The string \"%\" doesn't match the pattern\n" i
)
)
-- replacement
format "Pattern is : %\n" (regex2.toString())
for i in samples2 do
(
if regex2.ismatch(i) then
(
local replaced = regex2.replace i "another"
format "The string \"%\" matched the pattern, so it was replaced: \"%\"\n" i replaced
)
else
(
format "The string \"%\" does not match the pattern\n" i
)
)
- Output:
OK Pattern is : .*\bstring* OK The string "Some string 123" matches the pattern The string "Example text 123" doesn't match the pattern The string "string" matches the pattern The string "ThisString Will Not Match" doesn't match the pattern The string "A123,333,string" matches the pattern The string "123451" doesn't match the pattern OK Pattern is : \ba\b OK The string "I am a string" matched the pattern, so it was replaced: "I am another string" The string "And me too." does not match the pattern OK OK
MIRC Scripting Language
alias regular_expressions {
var %string = This is a string
var %re = string$
if ($regex(%string,%re) > 0) {
echo -a Ends with string.
}
%re = \ba\b
if ($regsub(%string,%re,another,%string) > 0) {
echo -a Result 1: %string
}
%re = \b(another)\b
echo -a Result 2: $regsubex(%string,%re,yet \1)
}
- Output:
Ends with string. Result 1: This is another string Result 2: This is yet another string
MUMPS
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.
REGEXP
NEW HI,W,PATTERN,BOOLEAN
SET HI="Hello, world!",W="world"
SET PATTERN=".E1"""_W_""".E"
SET BOOLEAN=HI?@PATTERN
WRITE "Source string - '"_HI_"'",!
WRITE "Partial string - '"_W_"'",!
WRITE "Pattern string created is - '"_PATTERN_"'",!
WRITE "Match? ",$SELECT(BOOLEAN:"YES",'BOOLEAN:"No"),!
;
SET BOOLEAN=$FIND(HI,W)
IF BOOLEAN>0 WRITE $PIECE(HI,W,1)_"string"_$PIECE(HI,W,2)
QUIT
Usage:
USER>D REGEXP^ROSETTA Source string - 'Hello, world!' Partial string - 'world' Pattern string created is - '.E1"world".E' Match? YES Hello, string!
NetRexx
/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.util.regex.
st1 = 'Fee, fie, foe, fum, I smell the blood of an Englishman'
rx1 = 'f.e.*?'
sbx = 'foo'
rx1ef = '(?i)'rx1 -- use embedded flag expression == Pattern.CASE_INSENSITIVE
-- using String's matches & replaceAll
mcm = (String st1).matches(rx1ef)
say 'String "'st1'"' 'matches pattern "'rx1ef'":' Boolean(mcm)
say
say 'Replace all occurrences of regex pattern "'rx1ef'" with "'sbx'"'
stx = Rexx
stx = (String st1).replaceAll(rx1ef, sbx)
say 'Input string: "'st1'"'
say 'Result string: "'stx'"'
say
-- using java.util.regex classes
pt1 = Pattern.compile(rx1, Pattern.CASE_INSENSITIVE)
mc1 = pt1.matcher(st1)
mcm = mc1.matches()
say 'String "'st1'"' 'matches pattern "'pt1.toString()'":' Boolean(mcm)
mc1 = pt1.matcher(st1)
say
say 'Replace all occurrences of regex pattern "'rx1'" with "'sbx'"'
sx1 = Rexx
sx1 = mc1.replaceAll(sbx)
say 'Input string: "'st1'"'
say 'Result string: "'sx1'"'
say
return
- Output:
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "(?i)f.e.*?": true Replace all occurrences of regex pattern "(?i)f.e.*?" with "foo" Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" Result string: "foo, foo, foo, fum, I smell the blood of an Englishman" String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "f.e.*?": true Replace all occurrences of regex pattern "f.e.*?" with "foo" Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" Result string: "foo, foo, foo, fum, I smell the blood of an Englishman"
NewLISP
(regex "[bB]+" "AbBBbABbBAAAA") -> ("bBBb" 1 4)
Nim
import re
var s = "This is a string"
if s.find(re"string$") > -1:
echo "Ends with string."
s = s.replace(re"\ a\ ", " another ")
echo s
Objeck
use RegEx;
bundle Default {
class RegExTest {
function : Main(args : String[]) ~ Nil {
string := "I am a string";
# exact match
regex := RegEx->New(".*string");
if(regex->MatchExact(".*string")) {
"ends with 'string'"->PrintLine();
};
# replace all
regex := RegEx->New(" a ");
regex->ReplaceAll(string, " another ")->PrintLine();
}
}
}
Objective-C
Test
NSString *str = @"I am a string";
NSString *regex = @".*string$";
// Note: the MATCHES operator matches the entire string, necessitating the ".*"
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([pred evaluateWithObject:str]) {
NSLog(@"ends with 'string'");
}
Unfortunately this method cannot find the location of the match or do substitution.
NSRegularExpressionSearch
Test
NSString *str = @"I am a string";
if ([str rangeOfString:@"string$" options:NSRegularExpressionSearch].location != NSNotFound) {
NSLog(@"Ends with 'string'");
}
Substitute
undocumented
NSString *orig = @"I am the original string";
NSString *result = [orig stringByReplacingOccurrencesOfString:@"original"
withString:@"modified"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [orig length])];
NSLog(@"%@", result);
NSRegularExpression
Test
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"string$"
options:0
error:NULL];
NSString *str = @"I am a string";
if ([regex rangeOfFirstMatchInString:str
options:0
range:NSMakeRange(0, [str length])
].location != NSNotFound) {
NSLog(@"Ends with 'string'");
}
Loop through matches
for (NSTextCheckingResult *match in [regex matchesInString:str
options:0
range:NSMakeRange(0, [str length])
]) {
// match.range gives the range of the whole match
// [match rangeAtIndex:i] gives the range of the i'th capture group (starting from 1)
}
Substitute
NSString *orig = @"I am the original string";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"original"
options:0
error:NULL];
NSString *result = [regex stringByReplacingMatchesInString:orig
options:0
range:NSMakeRange(0, [orig length])
withTemplate:@"modified"];
NSLog(@"%@", result);
OCaml
With the standard library
Test
#load "str.cma";;
let str = "I am a string";;
try
ignore(Str.search_forward (Str.regexp ".*string$") str 0);
print_endline "ends with 'string'"
with Not_found -> ()
;;
Substitute
#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" *)
Using Pcre
Library: ocaml-pcre
let matched pat str =
try ignore(Pcre.exec ~pat str); (true)
with Not_found -> (false)
;;
let () =
Printf.printf "matched = %b\n" (matched "string$" "I am a string");
Printf.printf "Substitute: %s\n"
(Pcre.replace ~pat:"original" ~templ:"modified" "I am the original string")
;;
Ol
; matching:
(define regex (string->regex "m/aa(bb|cc)dd/"))
(print (regex "aabbddx")) ; => true
(print (regex "aaccddx")) ; => true
(print (regex "aabcddx")) ; => false
; substitute part of a string:
(define regex (string->regex "s/aa(bb|cc)dd/HAHAHA/"))
(print (regex "aabbddx")) ; => HAHAHAx
(print (regex "aaccddx")) ; => HAHAHAx
(print (regex "aabcddx")) ; => false
ooRexx
/* Rexx */
/* Using the RxRegExp Regular Expression built-in utility class */
st1 = 'Fee, fie, foe, fum, I smell the blood of an Englishman'
rx1 = '[Ff]?e' -- unlike most regex engines, RxRegExp uses '?' instead of '.' to match any single character
sbx = 'foo'
myRE = .RegularExpression~new()
myRE~parse(rx1, MINIMAL)
mcm = myRE~pos(st1)
say 'String "'st1'"' 'matches pattern "'rx1'":' bool2string(mcm > 0)
say
-- The RxRegExp package doesn't provide a replace capability so you must roll your own
st0 = st1
loop label GREP forever
mcp = myRE~pos(st1)
if mcp > 0 then do
mpp = myRE~position
fnd = st1~substr(mcp, mpp - mcp + 1)
stx = st1~changestr(fnd, sbx, 1)
end
else leave GREP
st1 = stx
end GREP
say 'Input string: "'st0'"'
say 'Result string: "'stx'"'
return
exit
bool2string:
procedure
do
parse arg bv .
if bv then bx = 'true'
else bx = 'false'
return bx
end
exit
::requires "rxregexp.cls"
- Output:
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "[Ff]?e": true Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman" Result string: "foo, foo, foo, fum, I smell the blood of an Englishman"
Oxygene
// Match and Replace part of a string using a Regular Expression
//
// Nigel Galloway - April 15th., 2012
//
namespace re;
interface
type
re = class
public
class method Main;
end;
implementation
class method re.Main;
const
myString = 'I think that I am Nigel';
var
r: System.Text.RegularExpressions.Regex;
myResult : String;
begin
r := new System.Text.RegularExpressions.Regex('(I am)|(you are)');
Console.WriteLine("{0} contains {1}", myString, r.Match(myString));
myResult := r.Replace(myString, "you are");
Console.WriteLine("{0} contains {1}", myResult, r.Match(myResult));
end;
end.
Produces:
I think that I am Nigel contains I am I think that you are Nigel contains you are
Oz
declare
[Regex] = {Module.link ['x-oz://contrib/regex']}
String = "This is a string"
in
if {Regex.search "string$" String} \= false then
{System.showInfo "Ends with string."}
end
{System.showInfo {Regex.replace String " a " fun {$ _ _} " another " end}}
Pascal
// Match and Replace part of a string using a Regular Expression
//
// Nigel Galloway - April 11th., 2012
//
program RegularExpr;
uses
RegExpr;
const
myString = 'I think that I am Nigel';
myMatch = '(I am)|(you are)';
var
r : TRegExpr;
myResult : String;
begin
r := TRegExpr.Create;
r.Expression := myMatch;
write(myString);
if r.Exec(myString) then writeln(' contains ' + r.Match[0]);
myResult := r.Replace(myString, 'you are', False);
write(myResult);
if r.Exec(myResult) then writeln(' contains ' + r.Match[0]);
end.
Produces:
>RegularExpr I think that I am Nigel contains I am I think that you are Nigel contains you are
PascalABC.NET
begin
// text in tag (lazy quantification)
var s := '<tag>abc</tag> def <tag>ghi</tag>';
foreach var m in s.Matches('(?<=<tag>)(.*?)(?=</tag>)') do
Println(m.Value, m.Index);
// take words in parentheses
s := 'one two three four five';
Regex.Replace(s,'\w+','<$0>').Println;
end.
- Output:
abc 5 ghi 24 <one> <two> <three> <four> <five>
Perl
Test
$string = "I am a string";
if ($string =~ /string$/) {
print "Ends with 'string'\n";
}
if ($string !~ /^You/) {
print "Does not start with 'You'\n";
}
Substitute
$string = "I am a string";
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"
print $string;
In Perl 5.14+, you can return a new substituted string without altering the original string:
$string = "I am a string";
$string2 = $string =~ s/ a / another /r; # $string2 == "I am another string", $string is unaltered
print $string2;
Test and Substitute
$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";
}
Options
# add the following just after the last / for additional control
# g = globally (match as many as possible)
# i = case-insensitive
# s = treat all of $string as a single line (in case you have line breaks in the content)
# 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"
Omission of the regular expression binding operators
If regular expression matches are being made against the topic variable, it is possible to omit the regular expression binding operators:
$_ = "I like banana milkshake.";
if (/banana/) { # The regular expression binding operator is omitted
print "Match found\n";
}
Phix
with javascript_semantics include builtins\regex.e requires("1.0.2") -- (needs some recent bugfixes to regex.e for p2js) string s = "I am a string" printf(1,"\"%s\" %s with string\n",{s,iff(length(regex(`string$`,s))?"ends":"does not end")}) printf(1,"\"%s\" %s with You\n",{s,iff(length(regex(`^You`,s))?"starts":"does not start")}) ?gsub(`[A-Z]`,"abCDefG","*") ?gsub(`[A-Z]`,"abCDefGH","(&)") ?gsub(`[A-Z]+`,"abCDefGH","(&)") ?gsub(`string`,s,"replacement") s = gsub(`\ba\b`,s,"another") ?s ?gsub(`string`,s,"replacement")
- Output:
"I am a string" ends with string "I am a string" does not start with You "ab**ef*" "ab(C)(D)ef(G)(H)" "ab(CD)ef(GH)" "I am a replacement" "I am another string" "I am another replacement"
PHP
$string = 'I am a string';
# Test
if (preg_match('/string$/', $string))
{
echo "Ends with 'string'\n";
}
# Replace
$string = preg_replace('/\ba\b/', 'another', $string);
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";
- Output:
Ends with 'string' Foud 'a' and replaced it with 'another', resulting in this string: I am another string
PicoLisp
Calling the C library
PicoLisp doesn't have built-in regex functionality. It is easy to call the native C library.
(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 "\"") ) ) )
- Output:
String "a7z" matches pattern "a[0-9]z"
Using Pattern Matching
Regular expressions are static and inflexible. Another possibility is dynamic pattern matching, where arbitrary conditions can be programmed.
(let String "The number <7> is incremented"
(use (@A @N @Z)
(and
(match '(@A "<" @N ">" @Z) (chop String))
(format @N)
(prinl @A "<" (inc @) ">" @Z) ) ) )
- Output:
The number <8> is incremented
PowerShell
"I am a string" -match '\bstr' # true
"I am a string" -replace 'a\b','no' # I am no string
By default both the -match
and -replace
operators are case-insensitive. They can be made case-sensitive by using the -cmatch
and -creplace
operators.
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, ]*"
If CreateRegularExpression(1, regex$) And CreateRegularExpression(2, regex_replace$)
If MatchRegularExpression(1, String$)
Debug "Tags correct, and only alphanummeric or space characters between them"
EndIf
Debug ReplaceRegularExpression(2, String$, "char stuff")
EndIf
Python
import re
string = "This is a string"
if re.search('string$', string):
print("Ends with string.")
string = re.sub(" a ", " another ", string)
print(string)
R
First, define some strings.
pattern <- "string"
text1 <- "this is a matching string"
text2 <- "this does not match"
Matching with grep. The indices of the texts containing matches are returned.
grep(pattern, c(text1, text2)) # 1
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches.
regexpr(pattern, c(text1, text2))
[1] 20 -1 attr(,"match.length") [1] 6 -1
Replacement
gsub(pattern, "pair of socks", c(text1, text2))
[1] "this is a matching pair of socks" "this does not match"
Racket
#lang racket
(define s "I am a string")
(when (regexp-match? #rx"string$" s)
(displayln "Ends with 'string'."))
(unless (regexp-match? #rx"^You" s)
(displayln "Does not start with 'You'."))
(displayln (regexp-replace " a " s " another "))
Raku
(formerly Perl 6)
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
Raven
'i am a string' as str
Match:
str m/string$/
if "Ends with 'string'\n" print
Replace once:
str r/ a / another / print
str r/ /_/ print
Replace all:
str r/ /_/g print
Replace case insensitive:
str r/ A / another /i print
Splitting:
str s/ /
REBOL
REBOL [
Title: "Regular Expression Matching"
URL: http://rosettacode.org/wiki/Regular_expression_matching
]
string: "This is a string."
; REBOL doesn't use a conventional Perl-compatible regular expression
; syntax. Instead, it uses a variant Parsing Expression Grammar with
; the 'parse' function. It's also not limited to just strings. You can
; define complex grammars that actually parse and execute program
; files.
; Here, I provide a rule to 'parse' that specifies searching through
; the string until "string." is found, then the end of the string. If
; the subject string satisfies the rule, the expression will be true.
if parse string [thru "string." end] [
print "Subject ends with 'string.'"]
; For replacement, I take advantage of the ability to call arbitrary
; code when a pattern is matched -- everything in the parens will be
; executed when 'to " a "' is satisfied. This marks the current string
; location, then removes the offending word and inserts the replacement.
parse string [
to " a " ; Jump to target.
mark: (
remove/part mark 3 ; Remove target.
mark: insert mark " another " ; Insert replacement.
)
:mark ; Pick up where I left off.
]
print [crlf "Parse replacement:" string]
; For what it's worth, the above operation is more conveniently done
; with the 'replace' function:
replace string " another " " a " ; Change string back.
print [crlf "Replacement:" string]
- Output:
Subject ends with 'string.' Parse replacement: This is another string. Replacement: This is a string.
REXX
Rexx does not directly support the use of regular expressions as part of the language.
However, some rexx interpreters offer support for regular expressions via external function libraries or
through implementation specific extensions.
It is also possible to emulate regular expressions through appropriate coding techniques.
All of the following REXX examples are modeled after the PERL examples.
testing
/*REXX program demonstrates testing (modeled after Perl example).*/
$string="I am a string"
say 'The string is:' $string
x="string" ; if right($string,length(x))=x then say 'It ends with:' x
y="You" ; if left($string,length(y))\=y then say 'It does not start with:' y
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.*/
- Output:
The string is: I am a string It ends with: string It does not start with: You It contains the string: ring It does not contain the word: ring
substitution (destructive)
/*REXX program demonstrates substitution (modeled after Perl example).*/
$string = "I am a string"
old = " a "
new = " another "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
$string = changestr(old,$string,new)
say 'The changed string is:' $string
/*stick a fork in it, we're done.*/
- Output:
The original string is: I am a string old word is: a new word is: another The changed string is: I am another string
substitution (non-destructive)
/*REXX program shows non-destructive sub. (modeled after Perl example).*/
$string = "I am a string"
old = " a "
new = " another "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
$string2 = changestr(old,$string,new)
say 'The original string is:' $string
say 'The changed string is:' $string2
/*stick a fork in it, we're done.*/
- Output:
The original string is: I am a string old word is: a new word is: another The original string is: I am a string The changed string is: I am another string
test and substitute
/*REXX program shows test and substitute (modeled after Perl example).*/
$string = "I am a string"
old = " am "
new = " was "
say 'The original string is:' $string
say 'old word is:' old
say 'new word is:' new
if wordpos(old,$string)\==0 then
do
$string = changestr(old,$string,new)
say 'I was able to find and replace ' old " with " new
end
/*stick a fork in it, we're done.*/
- Output:
The original string is: I am a string old word is: am new word is: was I was able to find and replace am with was
Some older REXXes don't have a changestr BIF, so one is included here: ───► CHANGESTR.REX.
Ring
# Project : Regular expressions
text = "I am a text"
if right(text,4) = "text"
see "'" + text +"' ends with 'text'" + nl
ok
i = substr(text,"am")
text = left(text,i - 1) + "was" + substr(text,i + 2)
see "replace 'am' with 'was' = " + text + nl
Output:
'I am a text' ends with 'text' replace 'am' with 'was' = I was a text
Ruby
Test
str = "I am a string"
p "Ends with 'string'" if str =~ /string$/
p "Does not start with 'You'" unless str =~ /^You/
Substitute
str.sub(/ a /, ' another ') #=> "I am another string"
# Or:
str[/ a /] = ' another ' #=> "another"
str #=> "I am another string"
Substitute using block
str.gsub(/\bam\b/) { |match| match.upcase } #=> "I AM a string"
Run BASIC
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$
- Output:
'I am a string' ends with 'string' replace 'am' with 'was' = I was a string
Rust
Note that Regex::new
checks for a valid regex and thus returns a Result<Regex, Error>
.
use regex::Regex;
fn main() {
let s = "I am a string";
if Regex::new("string$").unwrap().is_match(s) {
println!("Ends with string.");
}
println!("{}", Regex::new(" a ").unwrap().replace(s, " another "));
}
Sather
Sather understands POSIX regular expressions.
class MAIN is
-- we need to implement the substitution
regex_subst(re:REGEXP, s, sb:STR):STR is
from, to:INT;
re.match(s, out from, out to);
if from = -1 then return s; end;
return s.head(from) + sb + s.tail(s.size - to);
end;
main is
s ::= "I am a string";
re ::= REGEXP::regexp("string$", true);
if re.match(s) then
#OUT + "'" + s + "'" + " ends with 'string'\n";
end;
if ~REGEXP::regexp("^You", false).match(s) then
#OUT + "'" + s + "'" + " does not begin with 'You'\n";
end;
#OUT + regex_subst(re, s, "integer") + "\n";
#OUT + regex_subst(REGEXP::regexp("am +a +st", true), s, "get the ") + "\n";
end;
end;
Scala
Define
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
Search and replace with string methods:
"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
Search with regex methods:
"\\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
Using pattern matching with regex:
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
|99 bottles of beer
|Take one down, pass it around
|98 bottles of beer on the wall""".stripMargin.lines
} line match {
case Bottles1(bottles) => println("There are still "+bottles+" bottles.") // full string must match, so this will match only once
case _ =>
}
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)
Replacing with regex:
Bottles2 replaceFirstIn ("99 bottles of beer", "98 bottles of beer")
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")
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.
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
Output
True
(R2D2,C3P0)
This is a story about R2D2 and Luke who are best friends.
Advanced example showing how to use capture groups within a pattern to reformat the names in a list.
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
Output
Potter, Harry – 98951212
Granger, Hermione – 59867125
Weasley, Ron – 56471832
Shiny
str: 'I am a string'
Match text:
if str.match ~string$~
say "Ends with 'string'"
end
Replace text:
say str.alter ~ a ~ 'another'
Sidef
Simple matching:
var str = "I am a string";
if (str =~ /string$/) {
print "Ends with 'string'\n";
}
Global matching:
var str = <<'EOF';
x:Foo
y:Bar
EOF
while (var m = str=~/(\w+):(\S+)/g) {
say "#{m[0]} -> #{m[1]}";
}
Substitutions:
var str = "I am a string";
# Substitute something mached by a regex
str.sub!(/ a /, ' another '); # "I am a string" => "I am another string"
# Remove something matched by a regex
str -= / \Kanother /i; # "I am another string" => "I am string"
# Global subtitution with a block
str = str.gsub(/(\w+)/, {|s1| 'x' * s1.len}); # globaly replace any word with 'xxx'
say str; # prints: 'x xx xxxxxx'
Slate
This library is still in its early stages. There isn't currently a feature to replace a substring.
'http://slatelanguage.org/test/page?query' =~ '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?'.
" ==> {'http:'. 'http'. '//slatelanguage.org'. 'slatelanguage.org'. '/test/page'. '?query'. 'query'. Nil} "
Smalltalk
|re s s1|
re := Regex fromString: '[a-z]+ing'.
s := 'this is a matching string'.
s1 := 'this does not match'.
(s =~ re)
ifMatched: [ :b |
b match displayNl
].
(s1 =~ re)
ifMatched: [ :b |
'Strangely matched!' displayNl
]
ifNotMatched: [
'no match!' displayNl
].
(s replacingRegex: re with: 'modified') displayNl.
|re s s1|
re := 'm[a-z]+ing' asRegex.
s := 'this is a matching string'.
(re search: s) ifTrue: [ 'matches!' ].
s1 := re copy: s replacingMatchesWith: 'modified'.
SNOBOL4
In SNOBOL4, patterns are based not on regular expressions, but are a native datatype which can be constructed, manipulated, concatenated, used in pattern expressions, stored into variables, and so forth. Patterns can be constructed ahead of time and saved in variables, and those preconstructed patterns can also reference additional pattern and data items which won't be known until actual pattern match time. Patterns can define calls to functions which will be called during actual pattern matching, and whose outcome can affect how the pattern match continues, which tentative matches will and won't be accepted, and so forth.
SNOBOL4 pattern matching is thus hugely more capable than traditional regular expressions are. An example of a pattern matching problem that would be prohibitively difficult to create as a regular expression would be to "create a pattern which matches a complete name and international postal mailing address."
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:
label subject pattern = object :(goto)
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.
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):
string1 = "The SNOBOL4 language is designed for string manipulation."
string1 "SNOBOL4" = "new SPITBOL" :s(changed)f(nochange)
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):
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
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.
Standard ML
There is no regex support in the Basis Library; however, various implementations have their own support.
Test
CM.make "$/regexp-lib.cm";
structure RE = RegExpFn (
structure P = AwkSyntax
structure E = BackTrackEngine);
val re = RE.compileString "string$";
val string = "I am a string";
case StringCvt.scanString (RE.find re) string
of NONE => print "match failed\n"
| SOME match =>
let
val {pos, len} = MatchTree.root match
in
print ("matched at position " ^ Int.toString pos ^ "\n")
end;
Stata
See regexm, regexr and regexs in Stata help.
scalar s="ars longa vita brevis"
* is there a vowel?
di regexm(s,"[aeiou]")
* replace the first vowel with "?"
di regexr(s,"[aeiou]","?")
Swift
RegularExpressionSearch
Test
import Foundation
let str = "I am a string"
if let range = str.rangeOfString("string$", options: .RegularExpressionSearch) {
println("Ends with 'string'")
}
Substitute (undocumented)
import Foundation
let orig = "I am the original string"
let result = orig.stringByReplacingOccurrencesOfString("original", withString: "modified", options: .RegularExpressionSearch)
println(result)
NSRegularExpression
Test
import Foundation
if let regex = NSRegularExpression(pattern: "string$", options: nil, error: nil) {
let str = "I am a string"
if let result = regex.firstMatchInString(str, options: nil, range: NSRange(location: 0, length: count(str.utf16))) {
println("Ends with 'string'")
}
}
Loop through matches
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)
}
Substitute
import Foundation
let orig = "I am the original string"
if let regex = NSRegularExpression(pattern: "original", options: nil, error: nil) {
let result = regex.stringByReplacingMatchesInString(orig, options: nil, range: NSRange(location: 0, length: count(orig.utf16)), withTemplate: "modified")
println(result)
}
Tcl
Test using regexp
:
set theString "I am a string"
if {[regexp -- {string$} $theString]} {
puts "Ends with 'string'"
}
if {![regexp -- {^You} $theString]} {
puts "Does not start with 'You'"
}
Extract substring using regexp
set theString "This string has >123< a number in it"
if {[regexp -- {>(\d+)<} $theString -> number]} {
puts "Contains the number $number"
}
Substitute using regsub
set theString = "I am a string"
puts [regsub -- { +a +} $theString { another }]
Toka
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.
#! Include the regex library
needs regex
#! The two test strings
" This is a string" is-data test.1
" Another string" is-data test.2
#! Create a new regex named 'expression' which tries
#! to match strings beginning with 'This'.
" ^This" regex: expression
#! An array to store the results of the match
#! (Element 0 = starting offset, Element 1 = ending offset of match)
2 cells is-array match
#! Try both test strings against the expression.
#! 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 .
TXR
Search and replace: simple
Txr is not designed for sed-like filtering, but here is how to do sed -e 's/dog/cat/g'
:
@(collect)
@(coll :gap 0)@mismatch@{match /dog/}@(end)@suffix
@(output)
@(rep)@{mismatch}cat@(end)@suffix
@(end)
@(end)
How it works is that the body of the coll
uses a double-variable match:
an unbound variable followed by a regex-match variable.
The meaning of this combination is, "Search for the regular expression, and if successful, then bind all the characters whcih were skipped over by the search to the first variable, and the matching text to the second variable."
So we collect pairs: pieces of mismatching text, and pieces of text which match the regex dog
.
At the end, there is usually going to be a piece of text which does not match the body, because it has no match for the regex.
Because :gap 0
is specified, the coll construct will terminate when faced with this nonmatching text, rather than skipping it in a vain search for a match, which allows @suffix
to take on this trailing text.
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 %
.
This allows for a very simple, straightforward regex which correctly matches C comments. The freeform
operator allows the entire input stream to be treated as one big line, so this works across multi-line comments.
@(freeform)
@(coll :gap 0)@notcomment@{comment /[/][*].%[*][/]/}@(end)@tail
@(output)
@(rep)@notcomment @(end)@tail
@(end)
Regexes in TXR Lisp
Parse regex at run time to abstract syntax:
$ txr -p '(regex-parse "a.*b")'
(compound #\a (0+ wild) #\b)
Dynamically compile regex abstract syntax to regex object:
$ txr -p "(regex-compile '(compound #\a (0+ wild) #\b))"
#<sys:regex: 9c746d0>
Search replace with regsub
.
$ txr -p '(regsub #/a+/ "-" "baaaaaad")'
"b-d"
UNIX Shell
bash and ksh implement regular expression matching via the [[ command's =~ operator.
ksh additionally allows regular expression as a flavour of general pattern matching.
Matching
s="I am a string"
if [[ $s =~ str..g$ ]]; then
echo "the string ends with 'str..g'"
fi
Replacing
Given these values
s="I am the original string"
re='o.*l'
repl="modified"
Can use regular expressions in parameter expansion
modified=${s/~(E)$re/$repl}
echo "$modified" # I am the modified string
have to break apart the original string to build the modified string.
if [[ $s =~ $re ]]; then
submatch=${BASH_REMATCH[0]}
modified="${s%%$submatch*}$repl${s#*$submatch}"
echo "$modified" # I am the modified string
fi
Vala
void main(){
string sentence = "This is a sample sentence.";
Regex a = new Regex("s[ai]mple"); // if using \n type expressions, use triple " for string literals as easy method to escape them
if (a.match(sentence)){
stdout.printf("\"%s\" is in \"%s\"!\n", a.get_pattern(), sentence);
}
string sentence_replacement = "cat";
sentence = a.replace(sentence, sentence.length, 0, sentence_replacement);
stdout.printf("Replaced sentence is: %s\n", sentence);
}
- Output:
"s[ai]mple" is in "This is a sample sentence."! Replaced sentence is: This is a cat sentence.
VBScript
Replace white spaces with line breaks.
text = "I need more coffee!!!"
Set regex = New RegExp
regex.Global = True
regex.Pattern = "\s"
If regex.Test(text) Then
WScript.StdOut.Write regex.Replace(text,vbCrLf)
Else
WScript.StdOut.Write "No matching pattern"
End If
- Input:
I need more coffee!!!
- Output:
I need more coffee!!!
Vedit macro language
Vedit can perform searches and matching with either regular expressions, pattern matching codes or plain text. These examples use regular expressions.
Match text at cursor location:
if (Match(".* string$", REGEXP)==0) {
Statline_Message("This line ends with 'string'")
}
Search for a pattern:
if (Search("string$", REGEXP+NOERR)) {
Statline_Message("'string' at and of line found")
}
Replace:
Replace(" a ", " another ", REGEXP+NOERR)
Web 68
@1Introduction.
Web 68 has access to a regular expression module
which can compile regular expressions,
use them for matching strings,
and replace strings with the matched string.
@a@<Compiler prelude@>
BEGIN
@<Declarations@>
@<Logic at the top level@>
END
@<Compiler postlude@>
@ The local compiler requires a special prelude.
@<Compiler prel...@>=
PROGRAM rosettacode regex CONTEXT VOID
USE regex,standard
@ And a special postlude.
@<Compiler post...@>=
FINISH
@1Regular expressions.
Compile a regular expression and match a string using it.
@<Decl...@>=
STRING regexp="string$";
REF REGEX rx=rx compile(regexp);
@ Declare a string for the regular expression to match.
@<Decl...@>=
STRING to match = "This is a string";
@ Define a routine to print the result of matching.
@<Decl...@>=
OP MATCH = (REF REGEX rx,STRING match)STRING:
IF rx match(rx,match,LOC SUBEXP)
THEN "matches"
ELSE "doesn't match"
FI;
@ Check whether the regular expression matches the string.
@<Logic...@>=
print(("String """,regexp,""" ",rx MATCH to match,
" string """,to match,"""",newline))
@ The end.
This program is processed by tang to produce Algol 68 code which has to be compiled by the a68toc compiler.
It's output is then compiled by gcc to produce a binary program. The script 'ca' provided with the Debian
package algol68toc requires the following command to process this program.
ca -l mod rosettacoderegex.w68
That's it. The resulting binary will print
'String "string$" matches string "This is a string"'
Wren
Version 1 (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.
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])")
}
- Output:
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
Version 2 (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.
/* 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])")
}
- Output:
Identical to first version.
zkl
The build in RE engine is a basic one.
Strings are immutable so replacement is creation.
var re=RegExp(".*string$");
re.matches("I am a string") //-->True
var s="I am a string thing"
re=RegExp("(string)") // () means group, ie if you see it, save it
re.search(s,True) //-->True, .search(x,True) means search for a match, ie don't need .*
p,n:=re.matched[0] //.matched-->L(L(7,6),"string")
String(s[0,p],"FOO",s[p+n,*]) //-->"I am a FOO thing"
re.search(s,True); // using .matched clears it
m:=re.matched[1];
s.replace(m,"FOO"); // -->"I am a FOO thing"
Using a mutable byte bucket:
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"