Jump to content

Regular expressions: Difference between revisions

Added BBC BASIC
(Added BBC BASIC)
Line 187:
abCDefGH
ab(CD)ef(GH)</lang>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Uses the [http://people.delphiforums.com/gjc/gnu_regex.html gnu_regex] library.
<lang bbcbasic> SYS "LoadLibrary", "gnu_regex.dll" TO gnu_regex%
IF gnu_regex% = 0 ERROR 100, "Cannot load gnu_regex.dll"
SYS "GetProcAddress", gnu_regex%, "regcomp" TO regcomp
SYS "GetProcAddress", gnu_regex%, "regexec" TO regexec
DIM regmatch{start%, finish%}, buffer% 256
REM Find all 'words' in a string:
teststr$ = "I love PATTERN matching!"
pattern$ = "([a-zA-Z]+)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
first% = 1
REPEAT
SYS regexec, buffer%, MID$(teststr$, first%), 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
PRINT "<" MID$(teststr$, first%+s%, f%-s%) ">"
first% += f%
ENDIF
UNTIL result%
REM Replace 'PATTERN' with 'pattern':
teststr$ = "I love PATTERN matching!"
pattern$ = "(PATTERN)"
SYS regcomp, buffer%, pattern$, 1 TO result%
IF result% ERROR 101, "Failed to compile regular expression"
SYS regexec, buffer%, teststr$, 1, regmatch{}, 0 TO result%
IF result% = 0 THEN
s% = regmatch.start%
f% = regmatch.finish%
MID$(teststr$, s%+1, f%-s%) = "pattern"
PRINT teststr$
ENDIF
 
SYS "FreeLibrary", gnu_regex%</lang>
'''Output:'''
<pre>
<I>
<love>
<PATTERN>
<matching>
I love pattern matching!
</pre>
 
=={{header|Bracmat}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.