Java regex for multiple string pattern in a string

Viewed 61

hi I need to validate if the string is having few mandatory words in it in an order in java

ISA*00*          *00*          *ZZ*FEDX           *08*123243432     *200905*0616*^*00501*1231231*0*P*:!GS*MZ*FEDX*123243432*20200905*0616*18859*X*005010!ST*240*188590001!BGN*00*123432423423234*20200905*061625**136634142!N1*SH*HUNTER FAN!N3*100 NEMAC WAY!N4*BYHALIA*MS*38611*US!LX*1!N1*CN*TEST S COMPANIES  INC.!N3*201 WASHINGTON STREET!N4*123123*MA*123123*US!L11*123432423423234*2I!LS*2710!MAN*CP*OD*SEEKONK*CP*ASA*US!L11*1231231*OQ!AT7*AM*BG***20200905*0654*LT!AT7*AG*BG***20200905*0000*LT!CD3*L*26.1!LE*2710!SE*18*12312312!ST*240*188590002!BGN*00*123123123*20200905*061625**150139758!N1*SH*HUNTER FAN!N3*100 NEMAC WAY!N4*1231231*MS*38611*US!LX*1!N1*CN*TEST S COMPANIES  INC.!N3*850 ROUTE 44!N4*ASDSDS*MA*02767*US!L11*1231231*2I!LS*2710!MAN*CP*FD*ASASAS*CP*ASAS*US!L11*12312312*OQ!AT7*X5*BG***20200905*0649*LT!AT7*AG*BG***20200905*0000*LT!CD3*L*24.2!LE*2710!SE*18*188590002!GE*2278*18859!IEA*1*000002724!

i wanted to check if the above string is having certain strings ( listed below ) in order ( the data has * in it ) , looking for a regex

ISA* , !GS* , !ST* ,!BGN* ,!LX* ,!L11* , !LE* ,!GE* ,!IEA* 

'^(ISA\*)+(!GS\*)+(!ST\*)*'

I tried the above but it didn't work .

Tried in this link https://www.freeformatter.com/java-regex-tester.html

1 Answers

Use

^ISA\*.*!GS\*.*!ST\*.*

See proof

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  ISA                      'ISA'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  !GS                      '!GS'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  !ST                      '!ST'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
Related