Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
43
Task/Regular-expressions/Ada/regular-expressions.adb
Normal file
43
Task/Regular-expressions/Ada/regular-expressions.adb
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;
|
||||
7
Task/Regular-expressions/Crystal/regular-expressions.cr
Normal file
7
Task/Regular-expressions/Crystal/regular-expressions.cr
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
s = "I am a string"
|
||||
# Match
|
||||
s =~ /string$/ # => 7
|
||||
s =~ /^You/ # => nil
|
||||
# Substitute
|
||||
s.sub(/\ba\b/, "a different") # => "I am a different string"
|
||||
s.gsub(/\b\w/) {|c| c.upcase } # => "I Am A String"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(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)))
|
||||
|
|
@ -1,7 +1,2 @@
|
|||
import Text.Regex
|
||||
|
||||
str = "I am a string"
|
||||
|
||||
case matchRegex (mkRegex ".*string$") str of
|
||||
Just _ -> putStrLn $ "ends with 'string'"
|
||||
Nothing -> return ()
|
||||
ghci> import Text.Regex.TDFA
|
||||
ghci> "I am a string" =~ ".*string$" :: Bool
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import Text.Regex
|
||||
|
||||
orig = "I am the original string"
|
||||
result = subRegex (mkRegex "original") orig "modified"
|
||||
putStrLn $ result
|
||||
ghci> import Text.Regex.TDFA
|
||||
ghci> compiled = makeRegex ".*string$" :: Regex
|
||||
ghci> match compiled "I am a string" :: Bool
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
; matching:
|
||||
(define regex (string->regex "m/aa(bb|cc)dd/"))
|
||||
(print (regex "aabbddx")) ; => true
|
||||
(print (regex "aaccddx")) ; => true
|
||||
(print (regex "aabcddx")) ; => false
|
||||
(print (m/aa(bb|cc)dd/ "aabbddx")) ; => true
|
||||
(print (m/aa(bb|cc)dd/ "aaccddx")) ; => true
|
||||
(print (m/aa(bb|cc)dd/ "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
|
||||
(print (s/aa(bb|cc)dd/HAHAHA/ "aabbddx")) ; => HAHAHAx
|
||||
(print (s/aa(bb|cc)dd/HAHAHA/ "aaccddx")) ; => HAHAHAx
|
||||
(print (s/aa(bb|cc)dd/HAHAHA/ "aabcddx")) ; => false
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
"I am a string" -match '\bstr' # true
|
||||
"I am a string" -replace 'a\b','no' # I am no string
|
||||
40
Task/Regular-expressions/Rebol/regular-expressions.rebol
Normal file
40
Task/Regular-expressions/Rebol/regular-expressions.rebol
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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]
|
||||
11
Task/Regular-expressions/V-(Vlang)/regular-expressions.v
Normal file
11
Task/Regular-expressions/V-(Vlang)/regular-expressions.v
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import regex
|
||||
|
||||
fn main() {
|
||||
mut r1 := regex.regex_opt("^.*string$")!
|
||||
mut r2 := regex.regex_opt("original")!
|
||||
s1 := "I am the original string"
|
||||
s3 := "replacement"
|
||||
s2 := r2.replace(s1, s3)
|
||||
if r1.matches_string(s1) {println("`${s1}` matches `${r1.query}`")} else {println("Failed!")}
|
||||
if s2 != s1 {println("`${s2}` replaces `${r2.query}` with `${s3}`")}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue