Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anybody used Regex in COBOL?

586 views
Skip to first unread message

pete dashwood

unread,
Jul 27, 2018, 8:03:53 PM7/27/18
to
Hi!

I am in a real hurry and would prefer not to have to go digging into MS
libraries if someone has already done so and can post a code snippet.

Basically, I am getting tired of writing long INSPECTs and IFs in COBOL
and would like to use some simple Regular Expressions instead.

A simple example (reality is much more complex...):

There is a path that might have a backslash at the end of it or it might
not. If it does, I want it left alone; if it doesn't, I want to change
it so there is a trailing backslash.

My COBOL solution (Maybe yours is better?):

[uses the Fujitsu intrinsic STORED-CHAR-LENGTH which returns the length
of the string without trailing spaces]

if Path(function STORED-CHAR-LENGTH(Path): 1) NOT = "\"
move "\" to Path(function STORED-CHAR-LENGTH(Path) + 1:)
end-if
(We still have trailing spaces, but at least we know the length we
actually want from the Path field...)

I guess 3 lines is not unreasonable, but it has to use a Language
extension to do it.

Without the Fujitsu intrinsic it would involve INSPECT (or a brute force
PERFORM loop) to find out where the last character is and the line count
goes up...

The following (C#) will put the backslash there if it is missing, but
leave it alone if it is there (2 lines of code):

Path = Path.Trim();
Path = Regex.Replace(path, @“(?<!\\)$”, “\\”);

(This is called a “reverse lookahead” in the trade... :-))

There are no trailing spaces in this solution.

Regular Expressions look like unintelligible gibberish but they are
actually a mathematical language of symbols that mean stuff, and they
are INCREDIBLY powerful. :-)

If anyone has managed to call the support for them from COBOL (access
the MS engine) could you post a snippet please?

Cheers,

Pete.
--
I used to write COBOL; now I can do anything...

Bruce Axtens

unread,
Jul 31, 2018, 1:02:37 AM7/31/18
to
On 7/28/2018 8:03 AM, pete dashwood wrote:
> and would like to use some simple Regular Expressions instead.

MicroFocus has regex, or at least that's what one website said. Which
dialect are you targeting?

Bruce.

---
This email has been checked for viruses by AVG.
https://www.avg.com

bwti...@gmail.com

unread,
Jul 31, 2018, 2:53:30 AM7/31/18
to
Zeev Atlas did a PCRE (Perl Compatible Regular Expression) binding to COBOL.

http://www.zaconsultants.net/16401.html

The link is about copybooks to ease direct access to PCRE functions. These will not be one liners. ;-)

The main PCRE binding on that site if for z/OS, but the copybook might work with just about any compiler variant. Again, not one liners.

You'd need better fencing on the COBOL STORE-CHAR-LENGTH usage, Peter, just in case Path wasn't quite large enough for the + 1 refmod.

Here's some FLI (foreign language intrinsic) COBOL, that is a one liner, heavy in use of extensions and your regex, taking into account the double escape problem of Python interpreting backslash, then the re engine doing the same. Everything doubles up to four each.

display python('import sys,re; result = re.sub("(?<!\\\\)$", "\\\\", sys.argv[1])', trim(Path trailing))

You need a ./configure --with-python build of GnuCOBOL for that to work though.

Or for in place update

move python('import sys,re; result = re.sub("(?<!\\\\)$", "\\\\", sys.argv[1])', trim(Path trailing)) to Path

Expensive, given the task at hand, not recommended really, but possible.

Have good

pete dashwood

unread,
Jul 31, 2018, 5:48:50 AM7/31/18
to
Thanks, Bruce.

It is Fujitsu Net and Power COBOL.

pete dashwood

unread,
Jul 31, 2018, 6:00:38 AM7/31/18
to
Very well spotted, Brian. Thanks. :-)

>
> Here's some FLI (foreign language intrinsic) COBOL, that is a one liner, heavy in use of extensions and your regex, taking into account the double escape problem of Python interpreting backslash, then the re engine doing the same. Everything doubles up to four each.

No, I don't think that's viable for me... :-)
>
> display python('import sys,re; result = re.sub("(?<!\\\\)$", "\\\\", sys.argv[1])', trim(Path trailing))
>
> You need a ./configure --with-python build of GnuCOBOL for that to work though.

At least you are supporting it...

>
> Or for in place update
>
> move python('import sys,re; result = re.sub("(?<!\\\\)$", "\\\\", sys.argv[1])', trim(Path trailing)) to Path
>
> Expensive, given the task at hand, not recommended really, but possible.

Very interesting, thanks.

I have used the MS Regex engine in the past, so I know it exists :-)
But I can't remember exactly how I linked to it and it would take me a
day to find the right libraries and get them into COBOL.

Maybe a simple, CALLable, COBOL pass-thru where you give it the Regex
and the target string and it returns the updated target or a Boolean if
you are only matching... Perhaps pass the Regex Groups back if you want
to be comprehensive. I thought I'd have tons of time in Retirement but
it isn't working out that way... Busier than ever :-)
>
> Have good
>
Thanks for the response, Brian, and best wishes for Gnu COBOL.

Bruce Axtens

unread,
Aug 3, 2018, 3:23:04 AM8/3/18
to
On 7/31/2018 6:00 PM, pete dashwood wrote:

> Maybe a simple, CALLable, COBOL pass-thru where you give it the Regex
> and the target string and it returns the updated target or a Boolean if
> you are only matching... Perhaps pass the Regex Groups back if you want
> to be comprehensive. I thought I'd have tons of time in Retirement but
> it isn't working out that way... Busier than ever :-)

So if in my ample (NOT!) spare time I wanted to pursue that idea, how do
our cobol's interface with .NET DLLs? I've downloaded the 30 Day
MicroFocus for Visual Studio 2017 and also the RainCode compiler.

Bruce Axtens

unread,
Aug 3, 2018, 5:03:17 AM8/3/18
to
On 8/3/2018 3:23 PM, Bruce Axtens wrote:
> our cobol's interface with .NET DLLs? I've downloaded the 30 Day

My grammar is atrocious today. "your COBOLs interface". Shows I've been
up till 3am with catastrophe management.
0 new messages