02-08-2012 2:59 AM
Hi all!
I have this issue..
I have to validate serial patters if each character is Alphanumeric and numeric, for example:
Format1 > AAAANNAAANAA
Format2 > AAANNANAANAA
Format3 > AAANNANAANA
Format4 > AAANNNAAANAA
Format5 > NNNNNNNN
Format6 > AANNNNAAANAA
Taking as a example for the first one "AAAANNAAANAA" if I receive the value "ABCD12EFG3HI" should be ok
BUT! if I receive "ABCDQ2EFG3HI" (the number 1 changed for a "Q" it shout send an error
I was cheking the regex [:digit:][:alnum:] but I was not success.
Can you help me please?
Best Regards
02-08-2012 4:18 PM
I am sure there would be more elegant way, but you can try like this:
DATA: lv_pattern TYPE char20.
DATA: lv_value TYPE char20.
lv_pattern = 'AAAANNAAANAA'.
lv_value = 'ABCD12EFG3HI'.
DATA: lv_len TYPE i.
lv_len = STRLEN( lv_pattern ).
DATA: lv_char TYPE char1.
DATA: lv_val_char TYPE char1.
DATA: lv_index TYPE i.
DATA: mlen TYPE i.
DATA: lv_regex TYPE string.
DATA: lv_failed TYPE flag.
DO lv_len TIMES.
lv_char = lv_pattern+lv_index(1).
lv_val_char = lv_value+lv_index(1).
IF lv_char = 'A'.
lv_regex = '[a-zA-Z]'.
ELSEIF lv_char = 'N'.
lv_regex = '[0-9]'.
ENDIF.
lv_index = lv_index + 1.
FIND REGEX lv_regex IN lv_val_char
MATCH LENGTH mlen.
IF sy-subrc NE 0.
WRITE: 'Error at position', lv_index,
'expecting', lv_regex, 'but found otherwise'.
lv_failed = 'X'.
EXIT.
ENDIF.
ENDDO.
IF lv_failed IS INITIAL.
WRITE: 'Looks good'.
ENDIF.
Regards,
Naimesh Patel
02-08-2012 5:49 AM
You say that the incoming serial no must be validated with all the patterns available ?
02-08-2012 3:31 PM
Yep, it must to be validated, for example, if I receive this serial: ABCD12EFG3HI I have to validate that the first four positions are alphanumeric, the sixth and seventh are numeric eighth ninth and tenth alphanumeric, and so on, based on this pattern "AAAANNAAANAA"
02-08-2012 4:18 PM
I am sure there would be more elegant way, but you can try like this:
DATA: lv_pattern TYPE char20.
DATA: lv_value TYPE char20.
lv_pattern = 'AAAANNAAANAA'.
lv_value = 'ABCD12EFG3HI'.
DATA: lv_len TYPE i.
lv_len = STRLEN( lv_pattern ).
DATA: lv_char TYPE char1.
DATA: lv_val_char TYPE char1.
DATA: lv_index TYPE i.
DATA: mlen TYPE i.
DATA: lv_regex TYPE string.
DATA: lv_failed TYPE flag.
DO lv_len TIMES.
lv_char = lv_pattern+lv_index(1).
lv_val_char = lv_value+lv_index(1).
IF lv_char = 'A'.
lv_regex = '[a-zA-Z]'.
ELSEIF lv_char = 'N'.
lv_regex = '[0-9]'.
ENDIF.
lv_index = lv_index + 1.
FIND REGEX lv_regex IN lv_val_char
MATCH LENGTH mlen.
IF sy-subrc NE 0.
WRITE: 'Error at position', lv_index,
'expecting', lv_regex, 'but found otherwise'.
lv_failed = 'X'.
EXIT.
ENDIF.
ENDDO.
IF lv_failed IS INITIAL.
WRITE: 'Looks good'.
ENDIF.
Regards,
Naimesh Patel