Jump to content

String matching: Difference between revisions

m
m (Category:Simple, {{out}})
Line 1:
{{task|String manipulation}} {{basic data operation}}
[[Category: String manipulation]] [[Category:Simple]]
Given two strings, demonstrate the following 3 types of matchings:
 
Line 33 ⟶ 34:
end Match_Strings;
</lang>
{{out}}
Sample output:
<pre>
'abcd' starts with 'ab'
Line 65 ⟶ 66:
b_seek(b, t) != -1,
b_seek(b, t) != -1 && b_seek(b, t) + length(t) == length(s));</lang>
{{out}}
Output:
<pre>starts with, embeds, ends with "Bang": 1, 1, 0
starts with, embeds, ends with "ok": 0, 1, 1
Line 93 ⟶ 94:
(string in string("ab",loc2,"abab"[loc+1:])|loc+loc2|-1) # returns +3 #
))</lang>
{{out}}
Output:
<pre>
TFFT -1 +1 +3
Line 205 ⟶ 206:
</lang>
 
{{out}}
Output:
'qwertyuiop' starts with 'qwerty'
'qwertyuiop' contains 'wert' at position 2
Line 264 ⟶ 265:
= N%
</lang>
{{out}}
Output:
<pre>"The fox jumps over the dog" starts with "The"
"The fox jumps over the dog" contains "The" at position 1
Line 285 ⟶ 286:
)
)</lang>
In the last line, Bracmat is forced by the always failing node <code>~</code> to backtrack until all occurrences of 'be' are found. Thereafter the pattern match expression fails. The interesting part is the side effect: while backtracking, the accumulator <code>N</code> keeps track of how many are found.
Thereafter the pattern match expression fails.
The interesting part is the side effect: while backtracking,
the accumulator <code>N</code> keeps track of how many are found.
 
{{out}}
Output:
<pre>sentence starts with 'I'
sentence contains 'such'
Line 327 ⟶ 331:
return 0;
}</lang>
{{out}}
Output :
<pre>Starts with Test ( Hello,Hell ) : 1
Ends with Test ( Code,ode ) : 1
Line 376 ⟶ 380:
return 0;
}</lang>
{{out}}
Output:<pre>matching `A Short String' with `ort S':
matched: pos 4
end match
Line 613 ⟶ 618:
</lang>
Execute: ^Q StringMatching.Do <br/>
{{out}}
Output:
<pre>
abcdefghijklmnopqrstuvwxyz startsWith abc :> $TRUE
Line 799 ⟶ 804:
end if</lang>
 
{{out}}
Output:
<pre>'qwertyuiop' starts with 'qwerty'
'qwertyuiop' contains 'wert' at position 2
'qwertyuiop' does not end with 'uio'
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 831 ⟶ 835:
|> Seq.iter (printfn "substring %A begins at position %d (zero-based)" contains)
0</lang>
{{out}}
Output
<pre>text = "一二三四五六七八九十"
starts with "一二": true
Line 881 ⟶ 885:
</lang>
 
{{out}}
Output:
<pre>
String is: Fantom Language
Line 913 ⟶ 917:
END SUB
</lang>
{{out}}
Output
<pre>"roko, mat jane do" starts with "roko"
"roko, mat jane do" contains "mat" at 7
Line 967 ⟶ 971:
}</lang>
 
Output{{out}} (in message boxes, 1 per line):
'qwertyuiop' starts with 'qwerty'
'qwertyuiop' contains 'wert' at position 0
'qwertyuiop' contains 1 instances of 'wert'
'qwertyuiop' does not end with 'random garbage'
 
=={{header|Go}}==
<lang go>package main
Line 1,003 ⟶ 1,008:
match("abracadabra", "abr")
}</lang>
{{out}}
Output:
<pre>
1. abracadabra starts with abr: true
Line 1,067 ⟶ 1,072:
end</lang>
 
{{out}}
Sample output:<pre>Matching s2 :="ab" within s1:= "abcdabab"
Test #1 beginning matches
Test #2 all matches at positions [ 1 5 7 ]
Line 1,145 ⟶ 1,151:
console.log("3: Does '"+stringA+"' end with '"+stringB+"'? " + ( q3 ? "Yes." : "No."))</lang>
 
{{out}}
Output:
<pre>1: Does 'tacoloco' start with 'co'? No.
2: Is 'co' contained in 'tacoloco'? Yes, at index 2.
Line 1,338 ⟶ 1,344:
EndWith["XYZaaabXYZaaaaXYZXYZ", "XYZ"]
StringPosition["XYZaaabXYZaaaaXYZXYZ", "XYZ"]</lang>
{{out}}
Output<pre>True
True
{{1,3},{8,10},{15,17},{18,20}}</pre>
Line 1,680 ⟶ 1,687:
}</lang>
 
{{out}}
Output:<pre> 'abcbcbcd' vs: ab bc cd zz
 
R_starts_with: Bool::True Bool::False Bool::False Bool::False
Line 1,936 ⟶ 1,944:
CloseConsole()
EndIf</lang>
{{out}}
Sample output:
<pre>1
0
Line 1,957 ⟶ 1,965:
[regex]::Matches("spicywiener", "i").count
</lang>
{{out}}
Output:
<pre>
True
Line 1,984 ⟶ 1,992:
(string-contains "abab" "ba")
</lang>
{{out}}
Output:
<pre>
#t
Line 2,044 ⟶ 2,052:
 
/*stick a fork in it, we're done.*/</lang>
'''output'''{{out}} when the following is specified (five Marx brothers): &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt>
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,057 ⟶ 2,065:
string A contains string B 3 times (at positions 10, 23, 24)
</pre>
'''output'''{{out}} when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Z </tt>
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,070 ⟶ 2,078:
string A contains string B 1 time (at position 21)
</pre>
'''output'''{{out}} when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Chi </tt>
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,083 ⟶ 2,091:
string A contains string B 1 time (at position 1)
</pre>
'''output'''{{out}} when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt>
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,139 ⟶ 2,147:
print "String:";s1$;" does ";a$;"end with:";sn4$</lang>
<pre>
{{out}}
Output:
String:abc def ghi klmnop does begin with:abc
String:abc def ghi klmnop does contain:ef
Line 2,176 ⟶ 2,184:
end func;</lang>
 
{{out}}
Output:
<pre>
TRUE
Line 2,272 ⟶ 2,280:
ENDIF
</lang>
{{out}}
Output:
<pre>string1 >Rosetta Code
string2 >Code
Line 2,352 ⟶ 2,360:
 
{{out}}
 
<pre>$ txr cmatch.txr -
123
Line 2,432 ⟶ 2,439:
{{omit from|bc|No string operations in bc}}
{{omit from|dc|No string operations in dc}}
 
[[Category: String manipulation]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.