簡體   English   中英

用於String的AllInnsensitive變量replaceAll(,)方法Java

[英]Case Insensitive variable for String replaceAll(,) method Java

任何人都可以幫我創建java中的變量的正則表達式,以便字符串變量將被視為不區分大小寫並用WINDOWS替換每個單詞,如Access,access等等嗎?

這是代碼:

$html=html.replaceAll(label, "WINDOWS");

請注意,label是一個字符串變量。

只需將“不區分大小寫”開關添加到正則表達式:

html.replaceAll("(?i)"+label, "WINDOWS");

注意:如果標簽可能包含具有特殊正則表達式重要性的字符,例如,如果標簽是".*" ,但您希望標簽被視為純文本(即不是正則表達式),請在標簽周圍添加正則表達式引號,或者

html.replaceAll("(?i)\\Q" + label + "\\E", "WINDOWS");

要么

html.replaceAll("(?i)" + Pattern.quote(label), "WINDOWS");

String.replaceAll等效於創建匹配器並調用其replaceAll方法,因此您可以執行類似這樣的操作以使其不區分大小寫:

html = Pattern.compile(label, Pattern.CASE_INSENSITIVE).matcher(html).replaceAll("WINDOWS");

請參閱: String.replaceAllPattern.compile JavaDocs

只需使用模式和匹配器。 這是代碼

Pattern p = Pattern.compile("Your word", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("String containing words");
String result = m.replaceAll("Replacement word");

使用模式很容易,因為它們不區分大小寫。

有關更多信息,請參閱

與正則表達式匹配

Java:模式和匹配器

我想但不確定你想要標簽是[Aa][cC][cC][eE][sS][sS]

或者做

html = Pattern.compile(lable, Pattern.CASE_INSENSITIVE)
        .matcher(html).replaceAll("WINDOWS");

暫無
暫無

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

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