简体   繁体   中英

Extract a substring using regex in Spring expression language

I have a String like

String 1 : abcdefgh{EID##00000000001234567890##EID}khkgfhjjh

String 2 : qwertyuiop{EID##00000000001234567890##EID}asdaff

and I want the output 1234567890 , if EID##...##EID is present in String1 , if not present it will search in string 2. If not present in both result will be 00000000

But I can use ONLY Spring Expression language.

I tried something like :

${String1}.indexOf('EID##') != -1 AND ${String1}.indexOf('##EID') != -1 AND (${String1}.indexOf('##EID') - ${String1}.indexOf('EID##'))> 5
? 
(${String1}.substring(${String1}.indexOf('EID##')+5,${String1}.indexOf('##EID'))).replaceAll("^0+", "") 
: 
(${String2}.indexOf('EID##') != -1 AND ${String2}.indexOf('##EID') != -1 AND (${String2}.indexOf('##EID') - ${String2}.indexOf('EID##'))> 5  
? 
(${String2}.substring(${String2}.indexOf('EID##')+5,${String2}.indexOf('##EID'))).replaceAll("^0+", "")
: 
'0000000000')

But the problem is I have a characters limit of 350.(This is 523 characters) Is there a simpler way to this?

I tried this, its working.

(${String1} matches '. EID##. ##EID.*') ? (${String1}.substring(${String1}.indexOf('EID##')+5,${String1}.indexOf('##EID'))).replaceAll("^0+", "") : ((${String2} matches '. EID##. ##EID.*') ? (${String2}.substring(${String2}.indexOf('EID##')+5,${String2}.indexOf('##EID'))).replaceAll("^0+", "") : '000000000')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM