basicuniversepick

Pattern Matching BASIC programming Language and Universe Database


I need to identify following patterns in string. - "2N':'2N':'2N" - "2N'-'2N'-'2N" - "2N'/'2N'/'2N" - "2N'/'2N'-'2N" AND SO ON.....

basically i want this pattern if written in Simple language 2 NUMBERS [: / -] 2 NUMBERS [: / -] 2 NUMBERS

So is there anyway by which i could write one pattern which will cover all the possible scenarios ? or else i have to write total 9 patterns and had to match all 9 patterns to string.... and it is not the scenario in my code , i have to match 4, 2 number digits separated by [: / -] to string for which i have towrite total 27 patterns. So for understanding purpose i have taken 3 ,2 digit scenario... Please help me...Thank you


Solution

  • Maybe you could try something like (Pick R83 style)

    OK = X MATCH "2N1X2N1X2N" AND X[3,1]=X[6,1] AND INDEX(":/-",X[3,1],1) > 0

    Where variable X is some input string like: 12-34-56

    Should set variable OK to 1 if validation passes, else 0 for any invalid format.

    This seems to get all your required validation into a single statement. I have assumed that the non-numeric characters have to be the same. If this is not true, the check could be changed to something like:

    OK = X MATCH "2N1X2N1X2N" AND INDEX(":/-",X[3,1],1) > 0 AND INDEX(":/-",X[6,1],1) > 0

    Ok, I guess the requirement of surrounding characters was not obvious to me. Still, it does not make it much harder. You just need to 'parse' the string looking for the first (I assume) such pattern (if any) in the input string. This can be done in a couple of lines of code. Here is a (rather untested ) R83 style test program:

    PROMPT ":"
    LOOP
      LOOP
        CRT 'Enter test string':
        INPUT S
      WHILE S # "" AND LEN(S) < 8 DO
        CRT "Invalid input! Hit RETURN to exit, or enter a string with >= 8 chars!"
      REPEAT
    UNTIL S = "" DO
      *
      * Look for 1st occurrence of pattern in string..
      CARDNUM = ""
      FOR I = 1 TO LEN(S)-7 WHILE CARDNUM = ""
        IF S[I,8] MATCH "2N1X2N1X2N" THEN
          IF INDEX(":/-",S[I+2,1],1) > 0 AND INDEX(":/-",S[I+5,1],1) > 0 THEN
            CARDNUM = S[I,8] ;* Found it!
          END ELSE I = I + 8
        END
      NEXT I
      *
      CRT CARDNUM
    REPEAT
    

    There is only 7 or 8 lines here that actually look for the card number pattern in the source/test string.