(Originally posted 2014-04-26.)
I’ll admit I’ve found regular expressions a bit of a struggle. I bet most people have. For me it’s a matter of lots of arcane symbols that don’t have any inherent meaning. Contrast with many programming languages, which do have some.
It’s also not the case I don’t understand the concepts.
Anyhow I’m edging towards the point where Production code will need to allow regexes. So I want to take a list of space-separated names and see which items match a given regular expression.
For example a list of address space names..
And so the FL (for “Filter List”) REXX EXEC was born.
The code is below. It uses grep to do the testing and BPXWUNIX invokes grep.
You invoke it with
TSO FL <regex> <list>
if you’ve put it in a suitable CLIST library. Mine is in my ISPF one. I can also call it from ISPF Option 6 or from Batch.
TSO FL CICS$ CICSA CICSB PRODCICS MYCICS
Will display the string
as these two items match the regular expression.
/* REXX */ parse arg mygrep mylist /* Create a temporary stem set with data to pass to grep via BPXWUNIX */ wds=words(mylist) do w=1 to words(mylist) tmpStem.w=word(mylist,w) end tmpStem.0=wds cmd='grep "'mygrep'"' /* Do the actual grep */ call bpxwunix cmd,tmpStem.,filter.,stderr. /* Turn returned stem set into space-separated list */ resultList="" do f=1 to filter.0 resultList=resultList filter.f end /* Print any error messages */ do e=1 to stderr.0 say stderr.e end say strip(resultList) exit
Obviously this could be modified to be a callable routine, or to use one. In this simple sample I thought it best to leave it as open code.
You could probably also find a way to pass parameters to grep like -i for case insensitivity.
One further refinement which is more of a stretch is handling list items with a space in them: You’d need to rewrite the bit that creates stem variables from the words in the list string. But for my purposes I’m looking at names which don’t have spaces in them.
Observant readers will spot this code is derived from Filtering REXX Query Results With BPXWUNIX but this version is easy to prototype with from the command line.
If you’re wanting to get started with regular expressions have a play with it. Enjoy!
See Towards A Pattern Explorer – Jobname Analysis where I’ve explored this before. ↩
I’ve no idea how to invoke it if the list parameter is long enough to be beyond what fits on one line – interactively. In Batch you can use + or – as a continuation character. Calling from REXX you can, of course, pass an arbitrarily long string. But for my testing a short list suffices. ↩