Java正则表达式用于在字符串中匹配多个字符串模式

huangapple go评论72阅读模式
英文:

Java regex for multiple string pattern in a string

问题

hi我需要在Java中验证字符串是否按顺序包含其中的一些必需单词

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

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

我尝试了上面的方法,但没有成功。

在此链接中尝试过
https://www.freeformatter.com/java-regex-tester.html

英文:

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

得分: 1

使用

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

查看证明

解释

--------------------------------------------------------------------------------
  ^                        字符串的开头
--------------------------------------------------------------------------------
  ISA                      'ISA'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       除了 \n 之外的任意字符(0 次或多次
                           (匹配尽可能多的次数))
--------------------------------------------------------------------------------
  !GS                      '!GS'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       除了 \n 之外的任意字符(0 次或多次
                           (匹配尽可能多的次数))
--------------------------------------------------------------------------------
  !ST                      '!ST'
--------------------------------------------------------------------------------
  \*                       '*'
--------------------------------------------------------------------------------
  .*                       除了 \n 之外的任意字符(0 次或多次
                           (匹配尽可能多的次数))
英文:

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))

huangapple
  • 本文由 发表于 2020年10月9日 05:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定