![](/img/trans.png)
[英]Java & Regex: Matching a substring that is not preceded by specific characters
[英]Java regex: replace a substring that is preceded by '$'
我想知道是否有合適的方法來替換前面帶有$
的 substring 。 例如,如果我們有如下字符串:
String s = "something variable = $firstname something something";
或者
String s = "something foo($firstname) something else";
我想替換所有出現的“變量”(即前面有$
),在這種情況下, $firstname
用'x'
(包括單引號)。 所以我們應該得到結果:
String s = "something variable = 'x' something something";
或者
String s = "something foo('x') something else";
但我不確定如何編寫一個可以做到這一點的正確正則表達式。
嘗試類似\$[a-zA-Z]+
$ 后跟 az,AZ 中的一個或多個字符
String s = "something variable = $firstname something something";
匹配$firstname
String s = s.replaceAll("<your regex>", "'x'");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.