2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,11 +1,16 @@
|
|||
{{basic data operation}}
|
||||
[[Category: String manipulation]] [[Category:Simple]]
|
||||
Given two strings, demonstrate the following 3 types of matchings:
|
||||
{{basic data operation}}
|
||||
[[Category: String manipulation]]
|
||||
[[Category:Simple]]
|
||||
|
||||
# Determining if the first string starts with second string
|
||||
# Determining if the first string contains the second string at any location
|
||||
# Determining if the first string ends with the second string
|
||||
;Task:
|
||||
Given two strings, demonstrate the following three types of string matching:
|
||||
|
||||
::# Determining if the first string starts with second string
|
||||
::# Determining if the first string contains the second string at any location
|
||||
::# Determining if the first string ends with the second string
|
||||
|
||||
<br>
|
||||
Optional requirements:
|
||||
# Print the location of the match for part 2
|
||||
# Handle multiple occurrences of a string for part 2.
|
||||
::# Print the location of the match for part 2
|
||||
::# Handle multiple occurrences of a string for part 2.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -7,12 +7,22 @@ String.starts_with?(s1, s3)
|
|||
String.starts_with?(s2, s3)
|
||||
# => false
|
||||
|
||||
String.contains?(s1, s3)
|
||||
# => true
|
||||
String.contains?(s2, s3)
|
||||
# => true
|
||||
|
||||
String.ends_with?(s1, s3)
|
||||
# => false
|
||||
String.ends_with?(s2, s3)
|
||||
# => true
|
||||
|
||||
String.contains?(s1, s3)
|
||||
# => true
|
||||
String.contains?(s2, s3)
|
||||
# => true
|
||||
|
||||
# Optional requirements:
|
||||
Regex.run(~r/#{s3}/, s1, return: :index)
|
||||
# => [{0, 2}]
|
||||
Regex.run(~r/#{s3}/, s2, return: :index)
|
||||
# => [{2, 2}]
|
||||
|
||||
Regex.scan(~r/#{s3}/, "abcabc", return: :index)
|
||||
# => [[{0, 2}], [{3, 2}]]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
begins_with("abcd","ab") #returns true
|
||||
startswith("abcd","ab") #returns true
|
||||
search("abcd","ab") #returns 1:2, indices range where string was found
|
||||
ends_with("abcd","zn") #returns false
|
||||
endswith("abcd","zn") #returns false
|
||||
ismatch(r"ab","abcd") #returns true where 1st arg is regex string
|
||||
julia>for r in each_match(r"ab","abab")
|
||||
println(r.offset)
|
||||
|
|
|
|||
3
Task/String-matching/Perl-6/string-matching-1.pl6
Normal file
3
Task/String-matching/Perl-6/string-matching-1.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$haystack.starts-with($needle) # True if $haystack starts with $needle
|
||||
$haystack.contains($needle) # True if $haystack contains $needle
|
||||
$haystack.ends-with($needle) # True if $haystack ends with $needle
|
||||
3
Task/String-matching/Perl-6/string-matching-2.pl6
Normal file
3
Task/String-matching/Perl-6/string-matching-2.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
so $haystack ~~ /^ $needle / # True if $haystack starts with $needle
|
||||
so $haystack ~~ / $needle / # True if $haystack contains $needle
|
||||
so $haystack ~~ / $needle $/ # True if $haystack ends with $needle
|
||||
2
Task/String-matching/Perl-6/string-matching-3.pl6
Normal file
2
Task/String-matching/Perl-6/string-matching-3.pl6
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
substr($haystack, 0, $needle.chars) eq $needle # True if $haystack starts with $needle
|
||||
substr($haystack, *-$needle.chars) eq $needle # True if $haystack ends with $needle
|
||||
1
Task/String-matching/Perl-6/string-matching-4.pl6
Normal file
1
Task/String-matching/Perl-6/string-matching-4.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
$haystack.match($needle, :g)».from; # List of all positions where $needle appears in $haystack
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
my @subs = (
|
||||
# Regex-based:
|
||||
sub R_contains ( Str $_, Str $s2 ) { ? m/ $s2 / },
|
||||
sub R_starts_with ( Str $_, Str $s2 ) { ? m/ ^ $s2 / },
|
||||
sub R_ends_with ( Str $_, Str $s2 ) { ? m/ $s2 $ / },
|
||||
|
||||
# Index-based:
|
||||
sub I_contains ( Str $_, Str $s2 ) { .index( $s2) .defined },
|
||||
sub I_starts_with ( Str $_, Str $s2 ) { my $m = .index( $s2); $m.defined and $m == 0 },
|
||||
sub I_ends_with ( Str $_, Str $s2 ) { my $m = .rindex($s2); $m.defined and $m == .chars - $s2.chars },
|
||||
|
||||
# Substr-based:
|
||||
sub S_starts_with ( Str $_, Str $s2 ) { .substr(0, $s2.chars) eq $s2 },
|
||||
sub S_ends_with ( Str $_, Str $s2 ) { .substr( *-$s2.chars) eq $s2 },
|
||||
|
||||
# Optional tasks:
|
||||
sub R_find ( Str $_, Str $s2 ) { $/.from if /$s2/ },
|
||||
sub R_find_all ( Str $_, Str $s2 ) {
|
||||
my @p = .match: /$s2/, :g;
|
||||
@p».from if @p;
|
||||
},
|
||||
);
|
||||
|
||||
my $str1 = 'abcbcbcd';
|
||||
my @str2s = < ab bc cd zz >;
|
||||
|
||||
say "'$str1' vs:".fmt('%15s '), @str2s.fmt('%-15s');
|
||||
for [1, 4, 6], [2, 5, 7], [0, 3], [8, 9] {
|
||||
say();
|
||||
for @subs[.list] -> $sub {
|
||||
say "{$sub.name}:".fmt('%15s '),
|
||||
@str2s.map({ ~$sub.($str1, $_) }).fmt('%-15s');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,28 @@
|
|||
/*REXX program demonstrates some basic character string testing. */
|
||||
parse arg a b /*obtain A and B from the C.L. */
|
||||
say 'string A = ' a /*display string A to terminal.*/
|
||||
say 'string B = ' b /* " " B " " */
|
||||
/*REXX program demonstrates some basic character string testing (for matching). */
|
||||
parse arg A B; LB=length(B) /*obtain A and B from the command line.*/
|
||||
say 'string A = ' A /*display string A to the terminal.*/
|
||||
say 'string B = ' B /* " " B " " " */
|
||||
say
|
||||
if left(A,length(b))==b then say 'string A starts with string B'
|
||||
else say "string A doesn't start with string B"
|
||||
if left(A, LB)==B then say 'string A starts with string B'
|
||||
else say "string A doesn't start with string B"
|
||||
say /* [↓] another method using COMPARE BIF*/
|
||||
/*╔══════════════════════════════════════════════════════════════════════════╗
|
||||
║ if compare(A,B)==LB then say 'string A starts with string B' ║
|
||||
║ else say "string A doesn't start with string B" ║
|
||||
╚══════════════════════════════════════════════════════════════════════════╝*/
|
||||
p=pos(B, A)
|
||||
if p==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B (starting in position' p")"
|
||||
say
|
||||
/*another method, however a wee bit obtuse. */
|
||||
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
|
||||
if compare(a,b)==length(b) then say 'string A starts with string B'
|
||||
else say "string A doesn't start with string B"
|
||||
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬*/
|
||||
/* [↑] above is a big comment. */
|
||||
p=pos(b,a)
|
||||
if p==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B (starting in position' p")"
|
||||
if right(A, LB)==b then say 'string A ends with string B'
|
||||
else say "string A doesn't end with string B"
|
||||
say
|
||||
if right(A,length(b))==b then say 'string A ends with string B'
|
||||
else say "string A doesn't end with string B"
|
||||
say
|
||||
Ps=''; p=0; do until p==0
|
||||
p=pos(b, a, p+1)
|
||||
if p\==0 then Ps = Ps',' p
|
||||
end /*until ···*/
|
||||
Ps=space(strip(Ps, 'L', ","))
|
||||
times=words(Ps)
|
||||
if times==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B ',
|
||||
times 'time'left('s',times>1),
|
||||
"(at position"left('s', times>1) Ps')'
|
||||
|
||||
/*stick a fork in it, we're done.*/
|
||||
$=; p=0; do until p==0; p=pos(B, A, p+1)
|
||||
if p\==0 then $=$',' p
|
||||
end /*until ···*/
|
||||
$=space(strip($,'L',",")) /*elide extra blanks and leading comma.*/
|
||||
#=words($)
|
||||
if #==0 then say "string A doesn't contain string B"
|
||||
else say 'string A contains string B ' # " time"left('s', #>1),
|
||||
"(at position"left('s', #>1) $")"
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
14
Task/String-matching/SNOBOL4/string-matching.sno
Normal file
14
Task/String-matching/SNOBOL4/string-matching.sno
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
s1 = 'abcdabefgab'
|
||||
s2 = 'ab'
|
||||
s3 = 'xy'
|
||||
OUTPUT = ?(s1 ? POS(0) s2) "1. " s2 " begins " s1
|
||||
OUTPUT = ?(s1 ? POS(0) s3) "1. " s3 " begins " s1 ;# fails
|
||||
|
||||
n = 0
|
||||
again s1 POS(n) ARB s2 @a :F(p3)
|
||||
OUTPUT = "2. " s2 " found at position "
|
||||
+ a - SIZE(s2) " in " s1
|
||||
n = a :(again)
|
||||
|
||||
p3 OUTPUT = ?(s1 ? s2 RPOS(0)) "3. " s2 " ends " s1
|
||||
END
|
||||
|
|
@ -1,18 +1,17 @@
|
|||
@(do
|
||||
(tree-case *args*
|
||||
((big small)
|
||||
(cond
|
||||
((< (length big) (length small))
|
||||
(put-line `@big is shorter than @small`))
|
||||
((str= big small)
|
||||
(put-line `@big and @small are equal`))
|
||||
((match-str big small)
|
||||
(put-line `@small is a prefix of @big`))
|
||||
((match-str big small -1)
|
||||
(put-line `@small is a suffix of @big`))
|
||||
(t (let ((pos (search-str big small)))
|
||||
(if pos
|
||||
(put-line `@small occurs in @big at position @pos`)
|
||||
(put-line `@small does not occur in @big`))))))
|
||||
(otherwise
|
||||
(put-line `usage: @(ldiff *full-args* *args*) <bigstring> <smallstring>`))))
|
||||
(tree-case *args*
|
||||
((big small)
|
||||
(cond
|
||||
((< (length big) (length small))
|
||||
(put-line `@big is shorter than @small`))
|
||||
((str= big small)
|
||||
(put-line `@big and @small are equal`))
|
||||
((match-str big small)
|
||||
(put-line `@small is a prefix of @big`))
|
||||
((match-str big small -1)
|
||||
(put-line `@small is a suffix of @big`))
|
||||
(t (let ((pos (search-str big small)))
|
||||
(if pos
|
||||
(put-line `@small occurs in @big at position @pos`)
|
||||
(put-line `@small does not occur in @big`))))))
|
||||
(otherwise
|
||||
(put-line `usage: @(ldiff *full-args* *args*) <bigstring> <smallstring>`)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue