2016-04-08 16 views
-4

Bir metin içinde (regex kullanarak) find ve replace yer tutucular istiyorum.Uzmanlar için Çok Basit Regex

String s = "This is a test {replace_me}. And this is another {replace_me_too}." 

s.replaceFirst(regex, newString); 

Nasıl olur ben "{*}" gibi bir dize ilk oluşumunu değiştirmek istediğinizde gibi regex bakış.

Bu

url = url.replaceFirst(Pattern.quote("{") + "*" + Pattern.quote("}"), param); 
+0

url = url.replaceFirst (Pattern.quote ("{") + "*" + Pattern.quote ("}"), param); ancak tüm arama dizesini değiştirmiyor. –

+1

sorunuza ekleyin ve daha sonra – rock321987

+0

bunun için bir cevabınız var mı? –

cevap

1

İsterseniz tüm oluşumları yerine replaceAll() işlevini kullanabilirsiniz çalıştılar.

Java Kod

String REGEX = "\\{[^}]*\\}"; 
String INPUT = "This is a test {replace_me}. And this is another {replace_me_too}."; 
String REPLACE = "xxx"; 
Pattern p = Pattern.compile(REGEX); 

Matcher m = p.matcher(INPUT); 
INPUT = m.replaceFirst(REPLACE); 
System.out.println(INPUT); 

Ideone Demo