簡體   English   中英

如何使用Regex和Java在這3種情況下提取字符串

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

我正在處理一個問題。 我有三個只想提取字符串的字符串。

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

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

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

在第1行的輸出將是3個字符串, Observações dacentral de créditoloja 第2行為空白,第3行為SimNão

我得到了像這樣的teste.replaceAll("\\\\#.+?\\\\}", "");teste.replaceAll("\\\\#.+?\\\\}", "");

有可能解決這個問題嗎?

謝謝

您的輸入不足以了解您的需求。 但是,您可以嘗試以下示例:

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

該示例說明了如何解析您的第一個輸入中的三個部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM