• Anybody used Regex in COBOL?

    From pete dashwood@dashwood@enternet.co.nz to comp.lang.cobol on Sat Jul 28 12:03:48 2018
    From Newsgroup: comp.lang.cobol

    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...
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Bruce Axtens@snetxa@hotmail.com to comp.lang.cobol on Tue Jul 31 13:02:14 2018
    From Newsgroup: comp.lang.cobol

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From bwtiffin@bwtiffin@gmail.com to comp.lang.cobol on Mon Jul 30 23:53:28 2018
    From Newsgroup: comp.lang.cobol

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From pete dashwood@dashwood@enternet.co.nz to comp.lang.cobol on Tue Jul 31 21:48:46 2018
    From Newsgroup: comp.lang.cobol

    On 31/07/2018 5:02 PM, Bruce Axtens wrote:
    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

    Thanks, Bruce.

    It is Fujitsu Net and Power COBOL.

    Pete.
    --
    I used to write COBOL; now I can do anything...
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From pete dashwood@dashwood@enternet.co.nz to comp.lang.cobol on Tue Jul 31 22:00:31 2018
    From Newsgroup: comp.lang.cobol

    On 31/07/2018 6:53 PM, bwtiffin@gmail.com wrote:
    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.

    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.

    Pete.
    --
    I used to write COBOL; now I can do anything...
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Bruce Axtens@snetxa@hotmail.com to comp.lang.cobol on Fri Aug 3 15:23:03 2018
    From Newsgroup: comp.lang.cobol

    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.

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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Bruce Axtens@snetxa@hotmail.com to comp.lang.cobol on Fri Aug 3 17:03:13 2018
    From Newsgroup: comp.lang.cobol

    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.

    Bruce.


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

    --- Synchronet 3.20a-Linux NewsLink 1.114