Regular expressions: Difference between revisions
Content added Content deleted
mNo edit summary |
|||
Line 167: | Line 167: | ||
Dalmatians: +100 +2 |
Dalmatians: +100 +2 |
||
Gives 101 dogs |
Gives 101 dogs |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
task 1: match a string against a regular expression (Hopper use POSIX): |
|||
<lang Amazing Hopper> |
|||
#include <hopper.h> |
|||
main: |
|||
expReg="[A-Z]{1,2}[0-9][0-9A-Z]? +[0-9][A-Z]{2}" |
|||
flag compile = REG_EXTENDED |
|||
flag match=0 |
|||
número de matches=10, T1=0 |
|||
{flag compile,expReg} reg compile(T1) // compile regular expression, pointed whit T1 |
|||
{flag match,número de matches,T1,"We are at SN12 7NY for this course"},reg match, // execute |
|||
println |
|||
reg free(T1) // free pointer to regular expression compiled. |
|||
exit(0) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
11 18 SN12 7NY |
|||
</pre> |
|||
Task 2: Hopper does not substitute using regular expressions, but using proper functions. |
|||
<lang Amazing Hopper> |
|||
#include <hopper.h> |
|||
main: |
|||
expReg="[A-Z]{1,2}[0-9][0-9A-Z]? +[0-9][A-Z]{2}" |
|||
cadena = "We are at SN12 7NY for this course" |
|||
flag compile = REG_EXTENDED |
|||
flag match=0 |
|||
número de matches=10, T1=0 |
|||
{flag compile,expReg} reg compile(T1) // compile regular expression, pointed whit T1 |
|||
{flag match,número de matches,T1,cadena},reg match, // execute |
|||
matches=0,mov(matches) |
|||
reg free(T1) // free pointer to regular expression compiled. |
|||
From=0, To=0, toSearch="" |
|||
[1,1]get(matches), mov(From) |
|||
[1,2]get(matches), mov(To) |
|||
[1,3]get(matches), mov(toSearch) |
|||
// substitute with "transform": |
|||
{"another thing",toSearch,cadena}transform, println |
|||
// substitute with "delete"/"insert": |
|||
{To}minus(From),plus(1), {From, cadena} delete, mov(cadena) |
|||
{From,"another thing",cadena}insert , println |
|||
exit(0) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
We are at another thing for this course |
|||
We are at another thing for this course |
|||
</pre> |
</pre> |
||