Regular expressions: Difference between revisions

m
Fixed lang tags.
(Added Scala)
m (Fixed lang tags.)
Line 7:
=={{header|AppleScript}}==
{{libheader|Satimage.osax}}
<lang applescript>try
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>
 
=={{header|ALGOL 68}}==
Line 24:
<!-- {{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}}
<lang algolalgol68>INT match=0, no match=1, out of memory error=2, other error=3;
 
STRING str := "i am a string";
Line 51:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
 
For example:<lang algolalgol68>FORMAT pattern = $ddd" "c("cats","dogs")$;
FORMAT pattern = $ddd" "c("cats","dogs")$;
FILE file; STRING book; associate(file, book);
on value error(file, (REF FILE f)BOOL: stop);
Line 71 ⟶ 70:
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox % foundpos := RegExMatch("Hello World", "World$")
MsgBox % foundposreplaced := RegExMatchRegExReplace("Hello World", "World$") , "yourself")</lang>
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")
</lang>
 
=={{header|AWK}}==
AWK supports regular expressions, which are typically marked up with slashes in front and back, and the "~" operator:
<lang awk>$ awk '{if($0~/[A-Z]/)print "uppercase detected"}'
abc
ABC
uppercase detected</lang>
As shorthand, a regular expression in the condition part fires if it matches an input line:
<lang awk>awk '/[A-Z]/{print "uppercase detected"}'
def
DeF
uppercase detected</lang>
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):
<lang awk>$ awk '{gsub(/[A-Z]/,"*");print}'
abCDefG
ab**ef*
$ awk '{gsub(/[A-Z]/,"(&)");print}'
abCDefGH
ab(C)(D)ef(G)(H)</lang>
This variant matches one or more uppercase letters in one round:
<lang awk>$ awk '{gsub(/[A-Z]+/,"(&)");print}'
abCDefGH
ab(CD)ef(GH)</lang>
 
=={{header|C}}==
Line 152 ⟶ 149:
 
{{libheader|Boost}}
<lang cpp> #include <iostream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
 
int main()
{
boost::regex re(".* string$");
std::string s = "Hi, I am a string";
 
// match the complete string
if (boost::regex_match(s, re))
std::cout << "The string matches.\n";
else
std::cout << "Oops - not found?\n";
 
// match a substring
boost::regex re2(" a.*a");
boost::smatch match;
if (boost::regex_search(s, match, re2))
{
std::cout << "Matched " << match.length()
boost::regex re(".* string$");
<< " characters starting at " << match.position() << ".\n";
std::string s = "Hi, I am a string";
std::cout << "Matched character sequence: \""
<< match.str() << "\"\n";
// match the complete string
}
if (boost::regex_match(s, re))
else
std::cout << "The string matches.\n";
else{
std::cout << "Oops - not found?\n";
}
 
// match a substring
// replace a substring
boost::regex re2(" a.*a");
std::string dest_string;
boost::smatch match;
boost::regex_replace(std::back_inserter(dest_string),
if (boost::regex_search(s, match, re2))
s.begin(), s.end(),
{
std::cout << "Matched " << match.length() re2,
<< " characters starting at "'m <<now match.position() <<a changed".\n");
std::cout << "Matcheddest_string character<< sequencestd: \"":endl;
}</lang>
<< match.str() << "\"\n";
}
else
{
std::cout << "Oops - not found?\n";
}
// replace a substring
std::string dest_string;
boost::regex_replace(std::back_inserter(dest_string),
s.begin(), s.end(),
re2,
"'m now a changed");
std::cout << dest_string << std::endl;
}</lang>
 
=={{header|C sharp|C#}}==
Line 235 ⟶ 232:
 
=={{header|D}}==
<lang d> import std.stdio, std.regexp;
 
void main() {
string s = "I am a string";
 
// Test:
if (search(s, r"string$"))
writefln("Ends with 'string'");
 
// Test, storing the regular expression:
auto re1 = RegExp(r"string$");
if (re1.search(s).test)
writefln("Ends with 'string'");
 
// Substitute:
writefln(sub(s, " a ", " another "));
 
// Substitute, storing the regular expression:
auto re2 = RegExp(" a ");
writefln(re2.replace(s, " another "));
}</lang>
 
Note that in std.string there are string functions to perform those string operations in a faster way.
 
=={{header|Erlang}}==
<lang erlang>match() ->
match() ->
String = "This is a string",
case re:run(String, "string$") of
Line 271 ⟶ 267:
String = "This is a string",
NewString = re:replace(String, " a ", " another ", [{return, list}]),
io:format("~s~n",[NewString]).</lang>
</lang>
 
=={{header|Forth}}==
{{libheader|Forth Foundation Library}}
Test/Match
<lang forth>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]</lang>
 
=={{header|Haskell}}==
Test
<lang haskell> import Text.Regex
 
str = "I am a string"
 
case matchRegex (mkRegex ".*string$") str of
Just _ -> putStrLn $ "ends with 'string'"
Nothing -> return ()</lang>
 
Substitute
<lang haskell> import Text.Regex
 
orig = "I am the original string"
result = subRegex (mkRegex "original") orig "modified"
putStrLn $ result</lang>
 
=={{header|J}}==
Line 318 ⟶ 313:
J's regex support is built on top of PCRE.
 
<lang j>load'regex' NB. Load regex library
str =: 'I am a string' NB. String used in examples.</lang>
 
Matching:
<lang j> '.*string$' rxeq str NB. 1 is true, 0 is false
1</lang>
1
 
Substitution:
<lang j> ('am';'am still') rxrplc str
I am still a string</lang>
 
=={{header|Java}}==
Line 335 ⟶ 330:
Test
 
<lang java> String str = "I am a string";
if (str.matches(".*string$")) {
System.out.println("ends with 'string'");
}</lang>
 
Substitute
 
<lang java> String orig = "I am the original string";
String result = orig.replaceAll("original", "modified");
// result is now "I am the modified string"</lang>
 
=={{header|JavaScript}}==
Test/Match
<lang javascript> 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);</lang>
 
Substitute
<lang javascript> var subject = "Hello world!";
 
// Perform a string replacement
// newSubject == "Replaced!"
var newSubject = subject.replace(re_PatternToMatch, "Replaced");</lang>
 
=={{header|M4}}==
<lang M4>regexp(`GNUs not Unix', `\<[a-z]\w+')
<lang M4>
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')</lang>
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')
</lang>
 
Output:
Line 386 ⟶ 379:
Test
{{works with|Mac OS X|10.4+}}
<lang objc>NSString *str = @"I am a string";
NSString *str = @"I am a string";
NSString *regex = @".*string$";
 
Line 394 ⟶ 386:
if ([pred evaluateWithObject:str]) {
NSLog(@"ends with 'string'");
}</lang>
}
</lang>
Unfortunately this method cannot find the location of the match or do substitution.
 
Line 401 ⟶ 392:
=== With the standard library ===
Test
<lang ocaml> #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 -> ()
;;</lang>
 
Substitute
<lang ocaml> #load "str.cma";;
let orig = "I am the original string";;
let result = Str.global_replace (Str.regexp "original") "modified" orig;;
(* result is now "I am the modified string" *)</lang>
 
=== Using Pcre ===
Line 467 ⟶ 458:
Test
 
<lang php> if (preg_match('/string$/', $string))
{
echo "Ends with 'string'\n";
}</lang>
 
Replace
 
<lang php> $string = preg_replace('/\ba\b/', 'another', $string);
echo "Found 'a' and replace it with 'another', resulting in this string: $string\n";</lang>
 
=={{header|PowerShell}}==
Line 495 ⟶ 486:
=={{header|R}}==
First, define some strings.
<lang R>pattern <- "string"
pattern <- "string"
text1 <- "this is a matching string"
text2 <- "this does not match"</lang>
</lang>
Matching with grep. The indices of the texts containing matches are returned.
<lang R>grep(pattern, c(text1, text2)) # 1</lang>
<lang R>
grep(pattern, c(text1, text2)) # 1
</lang>
Matching with regexpr. The positions of the starts of the matches are returned, along with the lengths of the matches.
<lang R>regexpr(pattern, c(text1, text2))</lang>
<lang R>
regexpr(pattern, c(text1, text2))
</lang>
[1] 20 -1
attr(,"match.length")
[1] 6 -1
Replacement
<lang R>gsub(pattern, "pair of socks", c(text1, text2))</lang>
<lang R>
gsub(pattern, "pair of socks", c(text1, text2))
</lang>
[1] "this is a matching pair of socks" "this does not match"
 
=={{header|Raven}}==
 
<lang raven>'i am a string' as str</lang>
 
Match:
 
<lang raven>str m/string$/
if "Ends with 'string'\n" print</lang>
 
Replace:
 
<lang raven>str r/ a / another / print</lang>
 
=={{header|Ruby}}==
Test
<lang ruby> string="I am a string"
puts "Ends with 'string'" if string[/string$/]
puts "Does not start with 'You'" if !string[/^You/]</lang>
 
Substitute
<lang ruby> puts string.gsub(/ a /,' another ')
#or
string[/ a /]='another'
puts string</lang>
 
Substitute using block
<lang ruby> puts(string.gsub(/\bam\b/) do |match|
puts "I found #{match}"
#place "was" instead of the match
"was"
end)</lang>
 
=={{header|Scala}}==
Line 554 ⟶ 537:
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</lang>
</lang>
 
Search and replace with string methods:
<lang scala>"99 bottles of beer" matches "(\\d+) bottles of beer" // the full string must match
"99 bottles of beer" replace ("99", "98") // Single replacement
"99 bottles of beer" replaceAll ("b", "B") // Multiple replacement</lang>
</lang>
 
Search with regex methods:
Line 569 ⟶ 550:
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</lang>
</lang>
 
Using pattern matching with regex:
Line 585 ⟶ 565:
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)</lang>
</lang>
 
Replacing with regex:
<lang scala>Bottles2 replaceFirstIn ("99 bottles of beer", "98 bottles of beer")
Bottles3 replaceAllIn ("99 bottles of beer", "98 bottles of beer")</lang>
</lang>
 
=={{header|Slate}}==
Line 597 ⟶ 575:
This library is still in its early stages. There isn't currently a feature to replace a substring.
 
<lang slate>(Regex Matcher newOn: '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?')
<lang slate>
(Regex Matcher newOn: '^(([^:/?#]+)\\:)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?')
`>> [match: 'http://slatelanguage.org/test/page?query'. subexpressionMatches]
 
"==> {"Dictionary traitsWindow" 0 -> 'http:'. 1 -> 'http'. 2 -> '//slatelanguage.org'.
3 -> 'slatelanguage.org'. 4 -> '/test/page'. 5 -> '?query'. 6 -> 'query'. 7 -> Nil}"</lang>
 
</lang>
 
=={{header|Smalltalk}}==
Line 652 ⟶ 627:
Toka's regular expression library allows for matching, but does not yet provide for replacing elements within strings.
 
<lang toka>#! 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 .</lang>
 
=={{header|Vedit macro language}}==
Line 676 ⟶ 651:
 
Match text at cursor location:
<lang vedit>if (Match(".* string$", REGEXP)==0) {
if (Match(".* string$", REGEXP)==0) {
Statline_Message("This line ends with 'string'")
}</lang>
}
</lang>
 
Search for a pattern:
<lang vedit>if (Search("string$", REGEXP+NOERR)) {
if (Search("string$", REGEXP+NOERR)) {
Statline_Message("'string' at and of line found")
}</lang>
}
</lang>
 
Replace:
<lang vedit>Replace(" a ", " another ", REGEXP+NOERR)</lang>
<lang vedit>
Replace(" a ", " another ", REGEXP+NOERR)
</lang>
Anonymous user