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