Anonymous user
Regular expressions: Difference between revisions
Add NetRexx implementation
m (→{{header|C}}) |
(Add NetRexx implementation) |
||
Line 814:
Match? YES
Hello, string!</pre>
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.util.regex.
st1 = 'Fee, fie, foe, fum, I smell the blood of an Englishman'
rx1 = 'f.e.*?'
sbx = 'foo'
rx1ef = '(?i)'rx1 -- use embedded flag expression == Pattern.CASE_INSENSITIVE
-- using String's matches & replaceAll
mcm = (String st1).matches(rx1ef)
say 'String "'st1'"' 'matches pattern "'rx1ef'":' Boolean(mcm)
say
say 'Replace all occurences of regex pattern "'rx1ef'" with "'sbx'"'
stx = Rexx
stx = (String st1).replaceAll(rx1ef, sbx)
say 'Input string: "'st1'"'
say 'Result string: "'stx'"'
say
-- using java.util.regex classes
pt1 = Pattern.compile(rx1, Pattern.CASE_INSENSITIVE)
mc1 = pt1.matcher(st1)
mcm = mc1.matches()
say 'String "'st1'"' 'matches pattern "'pt1.toString()'":' Boolean(mcm)
mc1 = pt1.matcher(st1)
say
say 'Replace all occurences of regex pattern "'rx1'" with "'sbx'"'
sx1 = Rexx
sx1 = mc1.replaceAll(sbx)
say 'Input string: "'st1'"'
say 'Result string: "'sx1'"'
say
return
</lang>
'''Output:'''
<pre>
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "(?i)f.e.*?": true
Replace all occurences of regex pattern "(?i)f.e.*?" with "foo"
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman"
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman"
String "Fee, fie, foe, fum, I smell the blood of an Englishman" matches pattern "f.e.*?": true
Replace all occurences of regex pattern "f.e.*?" with "foo"
Input string: "Fee, fie, foe, fum, I smell the blood of an Englishman"
Result string: "foo, foo, foo, fum, I smell the blood of an Englishman"
</pre>
=={{header|NewLISP }}==
|