String matching: Difference between revisions
Content added Content deleted
m (→{{header|BQN}}: Split definition and usage) |
|||
Line 3,449: | Line 3,449: | ||
</body> |
</body> |
||
</html></lang> |
</html></lang> |
||
=={{header|Picat}}== |
|||
The two most common predicate to use for string matching is find/4 (find a substring) or append/3 (a general purpose reversible predicate for concatenating/splitting lists/strings). Both predicates are non-deterministic and can yield multiple solutions, e.g. together with findall/2. |
|||
<lang Picat>import util. |
|||
go => |
|||
S1 = "string second", |
|||
S2 = "string", |
|||
% - Determining if the first string starts with second string |
|||
println("Using find/4"), |
|||
if find(S1,S2,1,_) then |
|||
println("S1 starts with S2") |
|||
else |
|||
println("S1 does not start with S2") |
|||
end, |
|||
println("Using append/3"), |
|||
if append(S2,_,S1) then |
|||
println("S1 starts with S2") |
|||
else |
|||
println("S1 does not start with S2") |
|||
end, |
|||
% - Determining if the first string contains the second string at any location |
|||
S3 = "this is a string", |
|||
S4 = "is a", |
|||
if find(S3,S4,Start4,End4) then |
|||
printf("S3 contains S4 at pos %d..%d\n", Start4,End4) |
|||
else |
|||
println("S3 does not contain S4") |
|||
end, |
|||
% - Determining if the first string ends with the second string |
|||
S5 = "this is a string", |
|||
S6 = "string", |
|||
if find(S5,S6,Start6,S5.length) then |
|||
printf("S5 ends with S6, startpos: %d\n",Start6) |
|||
else |
|||
println("S5 does not end with S6") |
|||
end, |
|||
if append(_,S6,S5) then |
|||
println("S5 ends with S6") |
|||
else |
|||
println("S5 does not end with S6") |
|||
end, |
|||
S7 = "this is a string or a string", |
|||
S8 = "a string", |
|||
All = findall([Start8,End8], find(S7,S8,Start8,End8)), |
|||
println(positions=All), |
|||
% searching for " " |
|||
All2 = findall([Start9,End9], find(S7," ",Start9,End9)), |
|||
println(positions=All2), |
|||
nl.</lang> |
|||
{{out}} |
|||
<pre>Using find/4 |
|||
S1 starts with S2 |
|||
Using append/3 |
|||
S1 starts with S2 |
|||
S3 contains S4 at pos 6..9 |
|||
S5 ends with S6, startpos: 11 |
|||
S5 ends with S6 |
|||
positions = [[9,16],[21,28]] |
|||
positions = [[5,5],[8,8],[10,10],[17,17],[20,20],[22,22]]</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |