


Hdebug(*yes)
*
dRegex pr 4500 extproc('REGEX')
dStringIn 2000 const
dRegExpIn 500 const
*
d
d retval ds 4500
d result1 1 2000
d result2 2001 4000
d error 4001 4500
*
dtests s 2000
dtestr s 500
*
c eval tests = 'I have a cat, but I like my dog -
c better. Pity the mouse is dead' + x'00'
c eval testr = '(mouse|cat|dog|wolf|bear|human)'
*
c eval retval = regex(tests:testr)
c dump
c move '1' *inlr
c return
Test Service Program (ILE RPG)
h debug thread(*serialize) nomain bnddir('QC2LE')
*
*****************************************************************
* Procedure Interface(s) *
*****************************************************************
*
dRegex pr 4500
dStringIn 2000
dRegExpIn 500
*
*****************************************************************
* Sub-procedure Regex *
*****************************************************************
*
PRegex b export
d pi 4500
dStringIn 2000
dRegExpIn 500
*
*****************************************************************
* Variable Specifications *
*****************************************************************
*
d retval ds
d result1 1 2000
d result2 2001 4000
d error 4001 4500
*
* Define classname constants
*
Dclass1 c 'com.robinsonway.uk.regex.Regex'
*
*
* Prototype constructor/method calls
*
DRegex PR O ExtProc(*JAVA:Class1:*constructor)
D Class(*JAVA:Class1)
* constructor parameters
D SearchString O Class(*JAVA:'java.lang.String')
D Const
D RegExp O Class(*JAVA:'java.lang.String')
D Const
*
DgetTotalMatches PR 5I 0 ExtProc(*JAVA:Class1:'getTotalMatche-
D s')
*
DgetStart PR 5I 0 ExtProc(*JAVA:Class1:'getStart')
* method parameters
Dindex 5I 0 Value
*
DgetEnd PR 5I 0 ExtProc(*JAVA:Class1:'getEnd')
* method parameters
Dindex 5I 0 Value
*
* newString constructor that accepts a byte array
* (an alphnumeric variable in RPG speak). It
* returns a string object.
*
D newString PR O ExtProc(*JAVA:
D 'java.lang.String':
D *CONSTRUCTOR)
D Class(*JAVA:'java.lang.String')
D bytes 9999A Const Varying
*
* Method variables
*
D sSearchString S O Class(*JAVA:'java.lang.String')
D sRegExp S O Class(*JAVA:'java.lang.String')
D RegObj S O Class(*JAVA:class1)
d entries s 5i 0
d start s 5i 0
d end s 5i 0
d idx s 5i 0
*
* Set the Java classpath etc
*
c call 'SETJVAENV'
c parm 'TALP' app 10
c parm 'A' aorr 1
*
* Instantiation of Regex Class
*
c eval sSearchString =
c newstring(%trim(StringIn))
c eval sRegExp =
c newstring(RegExpIn)
c eval RegObj = Regex(sSearchString:sRegExp)
*
c eval entries = getTotalMatches(RegObj)
*
c eval idx = 0
c 1 do entries
c eval start = getStart(RegObj:idx)
c eval end = getEnd(RegObj:idx)
c add 1 idx
c enddo
*
c dump
*
c move '1' *inlr
c return retval
*
p e
Test Java Program (compiled into Jar using Eclipse for PC)
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
public class Regex
{
private List <String> results;
private List <String> results_start;
private List <String> results_end;
private Matcher m;
private short matches;
public Regex(String searchString, String regExp)
{
System.out.println(searchString + ":" + regExp);
Pattern p = Pattern.compile(regExp);
System.out.println(p.pattern());
m = p.matcher(searchString);
results = new ArrayList<String>();
results_start = new ArrayList<String>();
results_end = new ArrayList<String>();
while (m.find())
{
System.out.println("Found a " + m.group() + ".");
results.add(m.group());
results_start.add("" + m.start());
results_end.add("" + m.end());
matches++;
}
}
public short getStart(short idx)
{
String startString = results_start.get(idx);
short start = Short.valueOf(startString);
start++;
return start;
}
public short getEnd(short idx)
{
String endString = results_end.get(idx);
return Short.valueOf(endString);
}
public short getTotalMatches()
{
return matches;
}
} 

Test RPG to call ILE RPG Service Program Hdebug(*yes) * dRegex pr 4500 extproc(‘REGEX’) dStringIn 2000 const dRegExpIn 500 const * d d retval ds 4500 d result1 1 2000 d result2 2001 4000 d error 4001 4500 * dtests s 2000 dtestr s 500 * c eval tests = ‘I have a cat, but I like my dog - c better. Pity the mouse is dead’ + x’00′ c eval testr = ‘(mouse|cat|dog|wolf|bear|human)’ * c eval retval = regex(tests:testr) c dump c move ’1′ *inlr c return