Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Validate serial patterns

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

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

3 REPLIES 3

kesavadas_thekkillath
Active Contributor
0 Kudos

You say that the incoming serial no must be validated with all the patterns available ?

0 Kudos

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"

naimesh_patel
Active Contributor
0 Kudos

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