0

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

Nitin Rathod
  • 159
  • 2
  • 15

3 Answers3

1

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.

stope19
  • 30
  • 5
  • hey Thanks for answering this could work......but here the string to which i am comparing is not target string , string is like this "comments comments sdgsghsdgsjk 'Credit card number' eghsgkjnhgks". string is like comments and in between those comments i have to find credit card number so it will not be possible to find exact position so we cant use index – Nitin Rathod Aug 19 '16 at 07:01
  • heyy that could work thanx for the code.....i would surely use that logic in my code...... – Nitin Rathod Aug 20 '16 at 12:20
  • If this addressed your question, perhaps you might consider 'accepting' this answer? – stope19 Aug 22 '16 at 06:17
0

Not quite perfect but how about 2N1X2N1X2N this gets you 2 number followed by 1 of any character followed by 2 numbers etc.

jbmonco
  • 130
  • 4
0

This might help:

 BIG.STRING  ="HELLO TILDE ~ CARD 12:34:56 IS IN THIS STRING"
 TEMP.STRING = BIG.STRING

 CONVERT "~:/-" TO "*~~~" IN TEMP.STRING           

 IF TEMP.STRING MATCHES '0X2N"~"2N"~"2N0X'  THEN

    FIRST.TILDE.POSN = INDEX(TEMP.STRING,"~",1)
    CARD.STRING      = BIG.STRING[FIRST.TILDE.POSN-2,8]
    PRINT CARD.STRING
 END
Keith
  • 1
  • 2