tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
3
Task/Regular-expressions/0DESCRIPTION
Normal file
3
Task/Regular-expressions/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
The goal of this task is
|
||||
* to match a string against a regular expression
|
||||
* to substitute part of a string using a regular expression
|
||||
2
Task/Regular-expressions/1META.yaml
Normal file
2
Task/Regular-expressions/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Text processing
|
||||
13
Task/Regular-expressions/ALGOL-68/regular-expressions-1.alg
Normal file
13
Task/Regular-expressions/ALGOL-68/regular-expressions-1.alg
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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;
|
||||
12
Task/Regular-expressions/ALGOL-68/regular-expressions-2.alg
Normal file
12
Task/Regular-expressions/ALGOL-68/regular-expressions-2.alg
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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$))
|
||||
4
Task/Regular-expressions/AWK/regular-expressions-1.awk
Normal file
4
Task/Regular-expressions/AWK/regular-expressions-1.awk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$ awk '{if($0~/[A-Z]/)print "uppercase detected"}'
|
||||
abc
|
||||
ABC
|
||||
uppercase detected
|
||||
4
Task/Regular-expressions/AWK/regular-expressions-2.awk
Normal file
4
Task/Regular-expressions/AWK/regular-expressions-2.awk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
awk '/[A-Z]/{print "uppercase detected"}'
|
||||
def
|
||||
DeF
|
||||
uppercase detected
|
||||
6
Task/Regular-expressions/AWK/regular-expressions-3.awk
Normal file
6
Task/Regular-expressions/AWK/regular-expressions-3.awk
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
$ awk '{gsub(/[A-Z]/,"*");print}'
|
||||
abCDefG
|
||||
ab**ef*
|
||||
$ awk '{gsub(/[A-Z]/,"(&)");print}'
|
||||
abCDefGH
|
||||
ab(C)(D)ef(G)(H)
|
||||
3
Task/Regular-expressions/AWK/regular-expressions-4.awk
Normal file
3
Task/Regular-expressions/AWK/regular-expressions-4.awk
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$ awk '{gsub(/[A-Z]+/,"(&)");print}'
|
||||
abCDefGH
|
||||
ab(CD)ef(GH)
|
||||
43
Task/Regular-expressions/Ada/regular-expressions.ada
Normal file
43
Task/Regular-expressions/Ada/regular-expressions.ada
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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
|
||||
22
Task/Regular-expressions/Argile/regular-expressions.argile
Normal file
22
Task/Regular-expressions/Argile/regular-expressions.argile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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:)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
MsgBox % foundpos := RegExMatch("Hello World", "World$")
|
||||
MsgBox % replaced := RegExReplace("Hello World", "World$", "yourself")
|
||||
40
Task/Regular-expressions/BBC-BASIC/regular-expressions.bbc
Normal file
40
Task/Regular-expressions/BBC-BASIC/regular-expressions.bbc
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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%
|
||||
|
|
@ -0,0 +1 @@
|
|||
@("fesylk789/35768poq2art":? (#<7:?n & out$!n & ~) ?)
|
||||
39
Task/Regular-expressions/C++/regular-expressions.cpp
Normal file
39
Task/Regular-expressions/C++/regular-expressions.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#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()
|
||||
<< " 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;
|
||||
boost::regex_replace(std::back_inserter(dest_string),
|
||||
s.begin(), s.end(),
|
||||
re2,
|
||||
"'m now a changed");
|
||||
std::cout << dest_string << std::endl;
|
||||
}
|
||||
15
Task/Regular-expressions/C-sharp/regular-expressions.cs
Normal file
15
Task/Regular-expressions/C-sharp/regular-expressions.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
43
Task/Regular-expressions/C/regular-expressions.c
Normal file
43
Task/Regular-expressions/C/regular-expressions.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#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;
|
||||
}
|
||||
10
Task/Regular-expressions/Clojure/regular-expressions.clj
Normal file
10
Task/Regular-expressions/Clojure/regular-expressions.clj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(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 "))
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(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'")))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(let* ((string "I am a string")
|
||||
(string (cl-ppcre:regex-replace " a " string " another ")))
|
||||
(write-line string))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(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'."))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[1]> (regexp:match "fox" "quick fox jumps")
|
||||
#S(REGEXP:MATCH :START 6 :END 9)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
[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"
|
||||
12
Task/Regular-expressions/D/regular-expressions.d
Normal file
12
Task/Regular-expressions/D/regular-expressions.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio, std.regex;
|
||||
|
||||
void main() {
|
||||
immutable string s = "I am a string";
|
||||
|
||||
// Test:
|
||||
if (!match(s, r"string$").empty)
|
||||
writeln("Ends with 'string'.");
|
||||
|
||||
// Substitute:
|
||||
replace(s, regex(" a "), " another ").writeln();
|
||||
}
|
||||
11
Task/Regular-expressions/Erlang/regular-expressions.erl
Normal file
11
Task/Regular-expressions/Erlang/regular-expressions.erl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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]).
|
||||
19
Task/Regular-expressions/Forth/regular-expressions.fth
Normal file
19
Task/Regular-expressions/Forth/regular-expressions.fth
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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"]
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
line =~ %s/Frank/Frink/g
|
||||
16
Task/Regular-expressions/Go/regular-expressions.go
Normal file
16
Task/Regular-expressions/Go/regular-expressions.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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)
|
||||
}
|
||||
54
Task/Regular-expressions/Groovy/regular-expressions-1.groovy
Normal file
54
Task/Regular-expressions/Groovy/regular-expressions-1.groovy
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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")
|
||||
|
|
@ -0,0 +1 @@
|
|||
println woodchuck.replaceAll(/c\w+k/, "CHUCK")
|
||||
11
Task/Regular-expressions/Groovy/regular-expressions-3.groovy
Normal file
11
Task/Regular-expressions/Groovy/regular-expressions-3.groovy
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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"))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import Text.Regex
|
||||
|
||||
str = "I am a string"
|
||||
|
||||
case matchRegex (mkRegex ".*string$") str of
|
||||
Just _ -> putStrLn $ "ends with 'string'"
|
||||
Nothing -> return ()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import Text.Regex
|
||||
|
||||
orig = "I am the original string"
|
||||
result = subRegex (mkRegex "original") orig "modified"
|
||||
putStrLn $ result
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
12
Task/Regular-expressions/Icon/regular-expressions.icon
Normal file
12
Task/Regular-expressions/Icon/regular-expressions.icon
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
procedure main()
|
||||
|
||||
s := "A simple string"
|
||||
p := "string$" # regular expression
|
||||
|
||||
s ? write(image(s),if ReFind(p) then " matches " else " doesn't match ",image(p))
|
||||
|
||||
s[j := ReFind(p,s):ReMatch(p,s,j)] := "replacement"
|
||||
write(image(s))
|
||||
end
|
||||
|
||||
link regexp # link to IPL regexp
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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";
|
||||
2
Task/Regular-expressions/J/regular-expressions-1.j
Normal file
2
Task/Regular-expressions/J/regular-expressions-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
load'regex' NB. Load regex library
|
||||
str =: 'I am a string' NB. String used in examples.
|
||||
2
Task/Regular-expressions/J/regular-expressions-2.j
Normal file
2
Task/Regular-expressions/J/regular-expressions-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
'.*string$' rxeq str NB. 1 is true, 0 is false
|
||||
1
|
||||
2
Task/Regular-expressions/J/regular-expressions-3.j
Normal file
2
Task/Regular-expressions/J/regular-expressions-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
('am';'am still') rxrplc str
|
||||
I am still a string
|
||||
1
Task/Regular-expressions/J/regular-expressions-4.j
Normal file
1
Task/Regular-expressions/J/regular-expressions-4.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
open'regex'
|
||||
4
Task/Regular-expressions/Java/regular-expressions-1.java
Normal file
4
Task/Regular-expressions/Java/regular-expressions-1.java
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
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'");
|
||||
}
|
||||
6
Task/Regular-expressions/Java/regular-expressions-2.java
Normal file
6
Task/Regular-expressions/Java/regular-expressions-2.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import java.util.regex.*;
|
||||
Pattern p = Pattern.compile("a*b");
|
||||
Matcher m = p.matcher(str);
|
||||
while (m.find()) {
|
||||
// use m.group() to extract matches
|
||||
}
|
||||
3
Task/Regular-expressions/Java/regular-expressions-3.java
Normal file
3
Task/Regular-expressions/Java/regular-expressions-3.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String orig = "I am the original string";
|
||||
String result = orig.replaceAll("original", "modified");
|
||||
// result is now "I am the modified string"
|
||||
15
Task/Regular-expressions/JavaScript/regular-expressions-1.js
Normal file
15
Task/Regular-expressions/JavaScript/regular-expressions-1.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
var subject = "Hello world!";
|
||||
|
||||
// Perform a string replacement
|
||||
// newSubject == "Replaced!"
|
||||
var newSubject = subject.replace(re_PatternToMatch, "Replaced");
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
julia> ismatch(r"^\s*(?:#|$)", "not a comment")
|
||||
false
|
||||
|
||||
julia> ismatch(r"^\s*(?:#|$)", "# a comment")
|
||||
true
|
||||
41
Task/Regular-expressions/Julia/regular-expressions-2.julia
Normal file
41
Task/Regular-expressions/Julia/regular-expressions-2.julia
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
julia> m = match(r"(a|b)(c)?(d)", "acd")
|
||||
RegexMatch("acd", 1="a", 2="c", 3="d")
|
||||
|
||||
julia> m.match
|
||||
"acd"
|
||||
|
||||
julia> m.captures
|
||||
3-element Union(UTF8String,ASCIIString,Nothing) Array:
|
||||
"a"
|
||||
"c"
|
||||
"d"
|
||||
|
||||
julia> m.offset
|
||||
1
|
||||
|
||||
julia> m.offsets
|
||||
3-element Int64 Array:
|
||||
1
|
||||
2
|
||||
3
|
||||
|
||||
julia> m = match(r"(a|b)(c)?(d)", "ad")
|
||||
RegexMatch("ad", 1="a", 2=nothing, 3="d")
|
||||
|
||||
julia> m.match
|
||||
"ad"
|
||||
|
||||
julia> m.captures
|
||||
3-element Union(UTF8String,ASCIIString,Nothing) Array:
|
||||
"a"
|
||||
nothing
|
||||
"d"
|
||||
|
||||
julia> m.offset
|
||||
1
|
||||
|
||||
julia> m.offsets
|
||||
3-element Int64 Array:
|
||||
1
|
||||
0
|
||||
2
|
||||
5
Task/Regular-expressions/Lua/regular-expressions.lua
Normal file
5
Task/Regular-expressions/Lua/regular-expressions.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
str1 = "This is a string!"
|
||||
str2 = "string"
|
||||
|
||||
print( str1:match( str2 ) )
|
||||
erg = str1:gsub( "a", "another" ); print( erg )
|
||||
2
Task/Regular-expressions/M4/regular-expressions.m4
Normal file
2
Task/Regular-expressions/M4/regular-expressions.m4
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
regexp(`GNUs not Unix', `\<[a-z]\w+')
|
||||
regexp(`GNUs not Unix', `\<[a-z]\(\w+\)', `a \& b \1 c')
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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)
|
||||
}
|
||||
13
Task/Regular-expressions/MUMPS/regular-expressions.mumps
Normal file
13
Task/Regular-expressions/MUMPS/regular-expressions.mumps
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
StringCases["I am a string with the number 18374 in me",RegularExpression["[0-9]+"]]
|
||||
StringReplace["I am a string",RegularExpression["I\\sam"] -> "I'm"]
|
||||
37
Task/Regular-expressions/NetRexx/regular-expressions.netrexx
Normal file
37
Task/Regular-expressions/NetRexx/regular-expressions.netrexx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* 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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#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 -> ()
|
||||
;;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#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" *)
|
||||
10
Task/Regular-expressions/OCaml/regular-expressions-3.ocaml
Normal file
10
Task/Regular-expressions/OCaml/regular-expressions-3.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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")
|
||||
;;
|
||||
17
Task/Regular-expressions/Objeck/regular-expressions.objeck
Normal file
17
Task/Regular-expressions/Objeck/regular-expressions.objeck
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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'");
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
NSString *str = @"I am a string";
|
||||
if ([str rangeOfString:@"string$" options:NSRegularExpressionSearch].location != NSNotFound) {
|
||||
NSLog(@"Ends with 'string'");
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
NSString *orig = @"I am the original string";
|
||||
NSString *result = [orig stringByReplacingOccurrencesOfString:@"original"
|
||||
withString:@"modified"
|
||||
options:NSRegularExpressionSearch
|
||||
range:NSMakeRange(0, [orig length])];
|
||||
NSLog(@"%@", result);
|
||||
10
Task/Regular-expressions/Objective-C/regular-expressions-4.m
Normal file
10
Task/Regular-expressions/Objective-C/regular-expressions-4.m
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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'");
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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)
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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);
|
||||
30
Task/Regular-expressions/Oxygene/regular-expressions.oxy
Normal file
30
Task/Regular-expressions/Oxygene/regular-expressions.oxy
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// 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.
|
||||
8
Task/Regular-expressions/Oz/regular-expressions.oz
Normal file
8
Task/Regular-expressions/Oz/regular-expressions.oz
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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}}
|
||||
9
Task/Regular-expressions/PHP/regular-expressions.php
Normal file
9
Task/Regular-expressions/PHP/regular-expressions.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$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";
|
||||
25
Task/Regular-expressions/Pascal/regular-expressions.pascal
Normal file
25
Task/Regular-expressions/Pascal/regular-expressions.pascal
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// 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.
|
||||
12
Task/Regular-expressions/Perl-6/regular-expressions.pl6
Normal file
12
Task/Regular-expressions/Perl-6/regular-expressions.pl6
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use v6;
|
||||
if 'a long string' ~~ /string$/ {
|
||||
say "It ends with 'string'";
|
||||
}
|
||||
|
||||
# substitution has a few nifty features
|
||||
|
||||
$_ = 'The quick Brown fox';
|
||||
s:g:samecase/\w+/xxx/;
|
||||
.say;
|
||||
# output:
|
||||
# Xxx xxx Xxx xxx
|
||||
8
Task/Regular-expressions/Perl/regular-expressions-1.pl
Normal file
8
Task/Regular-expressions/Perl/regular-expressions-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$string = "I am a string";
|
||||
if ($string =~ /string$/) {
|
||||
print "Ends with 'string'\n";
|
||||
}
|
||||
|
||||
if ($string !~ /^You/) {
|
||||
print "Does not start with 'You'\n";
|
||||
}
|
||||
3
Task/Regular-expressions/Perl/regular-expressions-2.pl
Normal file
3
Task/Regular-expressions/Perl/regular-expressions-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$string = "I am a string";
|
||||
$string =~ s/ a / another /; # makes "I am a string" into "I am another string"
|
||||
print $string;
|
||||
3
Task/Regular-expressions/Perl/regular-expressions-3.pl
Normal file
3
Task/Regular-expressions/Perl/regular-expressions-3.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$string = "I am a string";
|
||||
$string2 = $string =~ s/ a / another /r; # $string2 == "I am another string", $string is unaltered
|
||||
print $string2;
|
||||
4
Task/Regular-expressions/Perl/regular-expressions-4.pl
Normal file
4
Task/Regular-expressions/Perl/regular-expressions-4.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$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";
|
||||
}
|
||||
7
Task/Regular-expressions/Perl/regular-expressions-5.pl
Normal file
7
Task/Regular-expressions/Perl/regular-expressions-5.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# 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"
|
||||
4
Task/Regular-expressions/Perl/regular-expressions-6.pl
Normal file
4
Task/Regular-expressions/Perl/regular-expressions-6.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$_ = "I like banana milkshake.";
|
||||
if (/banana/) { # The regular expression binding operator is omitted
|
||||
print "Match found\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(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 "\"") ) ) )
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(let String "The number <7> is incremented"
|
||||
(use (@A @N @Z)
|
||||
(and
|
||||
(match '(@A "<" @N ">" @Z) (chop String))
|
||||
(format @N)
|
||||
(prinl @A "<" (inc @) ">" @Z) ) ) )
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
"I am a string" -match '\bstr' # true
|
||||
"I am a string" -replace 'a\b','no' # I am no string
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
9
Task/Regular-expressions/Python/regular-expressions.py
Normal file
9
Task/Regular-expressions/Python/regular-expressions.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import re
|
||||
|
||||
string = "This is a string"
|
||||
|
||||
if re.search('string$',string):
|
||||
print "Ends with string."
|
||||
|
||||
string = re.sub(" a "," another ",string)
|
||||
print string
|
||||
3
Task/Regular-expressions/R/regular-expressions-1.r
Normal file
3
Task/Regular-expressions/R/regular-expressions-1.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pattern <- "string"
|
||||
text1 <- "this is a matching string"
|
||||
text2 <- "this does not match"
|
||||
1
Task/Regular-expressions/R/regular-expressions-2.r
Normal file
1
Task/Regular-expressions/R/regular-expressions-2.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
grep(pattern, c(text1, text2)) # 1
|
||||
1
Task/Regular-expressions/R/regular-expressions-3.r
Normal file
1
Task/Regular-expressions/R/regular-expressions-3.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
regexpr(pattern, c(text1, text2))
|
||||
1
Task/Regular-expressions/R/regular-expressions-4.r
Normal file
1
Task/Regular-expressions/R/regular-expressions-4.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
gsub(pattern, "pair of socks", c(text1, text2))
|
||||
42
Task/Regular-expressions/REBOL/regular-expressions.rebol
Normal file
42
Task/Regular-expressions/REBOL/regular-expressions.rebol
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
REBOL [
|
||||
Title: "Regular Expression Matching"
|
||||
Author: oofoe
|
||||
Date: 2009-12-06
|
||||
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]
|
||||
15
Task/Regular-expressions/REXX/regular-expressions-1.rexx
Normal file
15
Task/Regular-expressions/REXX/regular-expressions-1.rexx
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/*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.*/
|
||||
10
Task/Regular-expressions/REXX/regular-expressions-2.rexx
Normal file
10
Task/Regular-expressions/REXX/regular-expressions-2.rexx
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/*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.*/
|
||||
11
Task/Regular-expressions/REXX/regular-expressions-3.rexx
Normal file
11
Task/Regular-expressions/REXX/regular-expressions-3.rexx
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/*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.*/
|
||||
14
Task/Regular-expressions/REXX/regular-expressions-4.rexx
Normal file
14
Task/Regular-expressions/REXX/regular-expressions-4.rexx
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/*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.*/
|
||||
11
Task/Regular-expressions/Racket/regular-expressions.rkt
Normal file
11
Task/Regular-expressions/Racket/regular-expressions.rkt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#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 "))
|
||||
|
|
@ -0,0 +1 @@
|
|||
'i am a string' as str
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
str m/string$/
|
||||
if "Ends with 'string'\n" print
|
||||
|
|
@ -0,0 +1 @@
|
|||
str r/ a / another / print
|
||||
3
Task/Regular-expressions/Ruby/regular-expressions-1.rb
Normal file
3
Task/Regular-expressions/Ruby/regular-expressions-1.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
str = "I am a string"
|
||||
p "Ends with 'string'" if str =~ /string$/
|
||||
p "Does not start with 'You'" unless str =~ /^You/
|
||||
4
Task/Regular-expressions/Ruby/regular-expressions-2.rb
Normal file
4
Task/Regular-expressions/Ruby/regular-expressions-2.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
str.sub(/ a /, ' another ') #=> "I am another string"
|
||||
# Or:
|
||||
str[/ a /] = ' another ' #=> "another"
|
||||
str #=> "I am another string"
|
||||
1
Task/Regular-expressions/Ruby/regular-expressions-3.rb
Normal file
1
Task/Regular-expressions/Ruby/regular-expressions-3.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
str.gsub(/\bam\b/) { |match| match.upcase } #=> "I AM a string"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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$
|
||||
|
|
@ -0,0 +1 @@
|
|||
label subject pattern = object :(goto)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
string1 = "The SNOBOL4 language is designed for string manipulation."
|
||||
string1 "SNOBOL4" = "new SPITBOL" :s(changed)f(nochange)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue