I don't understand. A ^  at the beginning of a character class is a Not. The 
regex should match a string of invalid characters, or fail if there are none.

________________________________________
From: IBM Mainframe Discussion List <[email protected]> on behalf of 
Eric Erickson <[email protected]>
Sent: Friday, June 30, 2023 10:56 AM
To: [email protected]
Subject: regex always returns 1

I'm having an issue with trying to use a regular expression to validate an z/OS 
Dataset Name Qualifier. I traverse the DSN using strtok to extract a pointer to 
each qualifier. I built a regular expression that works using a web tool to 
identify any invalid characters. The string I build using the web tool is 
"[^A-Z0-9@#\$\}\-]+" which when I bring that up the mainframe I change to 
"[^A-Z0-9@#\\$\\}\\-]+" as I have to double the escape (\) otherwise the 
compiler issues a message.

Even when I shorted the regex to ""[^A-Z0-9]+" is returns a 1 for both strings 
(SCHEMA or SCH!MA) where I would expect the first to return 1 and second to 
return 0.

Basically I'm trying to validate that a string does not contain any lower case 
characters or any of the symbols except ($, @, #, -, or }).

Here is a subset of the code.

if (regcomp(&pRegExp, pszDsnPattern, REG_NOSUB) == 0)
{
   printf("ZDP2999T: RegEx compiled %s.\n", pszDsnPattern);
   /*
   **  Regular Expression compiled, make a copy of the buffer, as strtok
   **  is going to destroy it during tokenization.
   */
   strcpy(cBuffer, pszKeyValue);
   /*
   **  Get the first token in the string.
   */
   pszToken = strtok(cBuffer, ".");
   /*
   **  And loop through the string processing each returned string.
   */
   do
   {
      /*
      **  Perform z/OS Dataset Name Validation. Check the qualifier length
      **  and first character for validity.
      */
      printf("ZDP2999T: Qualifier \"%s\".\n", pszToken);
      if (strlen(pszToken) <= 8 &&amp; (isupper(pszToken[0]) || pszToken[0] == 
'$' ||
          pszToken[0] == '@' || pszToken[0] == '#'))
      {
         printf("ZDP2999T: Valid first char %c.\n", pszToken[0]);
         if ((iRc = regexec(&pRegExp, pszToken, 0, NULL, 0)) == 0)
         {
               bValidDbHlq = FALSE;
         }
      }
      else{
       bValidDbHlq = FALSE;
    }
 } while ((pszToken = strtok(NULL, ".")) &&amp; bValidDbHlq);
 /*
 **  Free the regex allocated storage.
 */
 regfree(&pRegExp);

Note: I do handle the special case of the first character via a separate test, 
but don't want to iterate through the remaining characters in a loop.

This is all under XL C under z/OS 2.5.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to