Regular expressions: Difference between revisions
Content added Content deleted
(objective-c matching) |
|||
Line 19: | Line 19: | ||
end try |
end try |
||
=={{header| |
=={{header|ALGOL 68}}== |
||
The routines ''grep in strings'' and ''sub in string'' are not part of [[ALGOL 68]]'s standard prelude. |
|||
<!-- {{does not work with|ALGOL 68|Standard - grep/sub in string are not part of the standard's prelude. }} --> |
|||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
|||
<lang algol>INT match=0, no match=1, out of memory error=2, other error=3; |
|||
STRING str := "i am a string"; |
|||
# Match: # |
|||
STRING m := "string$"; |
|||
INT start, end; |
|||
IF grep in string(m, str, start, end) = match THEN printf(($"Ends with """g""""l$, str[start:end])) FI; |
|||
# Replace: # |
|||
IF sub in string(" a ", " another ",str) = match THEN printf(($gl$, str)) FI;</lang> |
|||
Output: |
|||
<pre> |
|||
Ends with "string" |
|||
i am another string |
|||
</pre> |
|||
Standard ALGOL 68 does have an primordial form of pattern matching |
|||
called a '''format'''. This is designed to extract values from input data. |
|||
But it can also be used for outputting (and transputting) the original data. |
|||
{{works with|ALGOL 68|Standard - But declaring ''book'' as [][][]'''char''' }} |
|||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
|||
For example:<lang algol> |
|||
FORMAT pattern = $ddd" "c("cats","dogs")$; |
|||
FILE file; STRING book; associate(file, book); |
|||
on value error(file, (REF FILE f)BOOL: stop); |
|||
on format error(file, (REF FILE f)BOOL: stop); |
|||
book := "100 dogs"; |
|||
STRUCT(INT count, type) dalmatians; |
|||
getf(file, (pattern, dalmatians)); |
|||
print(("Dalmatians: ", dalmatians, new line)); |
|||
count OF dalmatians +:=1; |
|||
printf(($"Gives: "$, pattern, dalmatians, $l$))</lang> |
|||
Output: |
|||
<pre> |
|||
Dalmatians: +100 +2 |
|||
Gives 101 dogs |
|||
</pre> |
|||
=={{header|C}}== |
|||
{{works with|POSIX}} |
{{works with|POSIX}} |
||