简体   繁体   中英

How to extract the String in these 3 cases using Regex with Java

I´m dealing with one problem. I have three strings that I want to extract only the String.

Observações da #{loginBean.centralCreditoRole ? 'central de crédito' : 'loja'}

#{resource['images:header_logo_client.png']}

#{propostaCartaoBean.pojo.inss.exclusaoDataprevAgendada ? 'Sim' : 'Não'}

In line 1 the output will be 3 strings, Observações da , central de crédito , loja . Line 2 will be blank and Line 3 Sim and Não .

I got line 2 like this teste.replaceAll("\\\\#.+?\\\\}", "");

Is it possible to deal this problem?

Thanks

Your input is not sufficient enough to understand your need. However, you can try this example:

String input = "Observações da #{loginBean.centralCreditoRole ? 'central de crédito' : 'loja'}";
Pattern pattern = Pattern.compile("(.*?)#.*?\\?\\s*(?:'(.*?)(?<!\\\\)'\\s+:\\s+'(.*?)(?<!\\\\)')?");

Matcher m = pattern.matcher(input);
while (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
}

This one is showing how it can parse the three portions from your first input.

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