and would like to use some simple Regular Expressions instead.
Hi!Zeev Atlas did a PCRE (Perl Compatible Regular Expression) binding to COBOL. http://www.zaconsultants.net/16401.html
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...
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
On Friday, July 27, 2018 at 8:03:53 PM UTC-4, pete dashwood wrote:
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...
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
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 :-)
our cobol's interface with .NET DLLs? I've downloaded the 30 Day
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,030 |
Nodes: | 10 (0 / 10) |
Uptime: | 29:56:10 |
Calls: | 13,346 |
Calls today: | 3 |
Files: | 186,574 |
D/L today: |
5,132 files (1,258M bytes) |
Messages: | 3,357,859 |