回答
String s = "[email protected]";
String email = s.replaceAll("\\+.*@", "@");
使用正則表達式是:
String repl = str.replaceAll("(.*?)[+].*?(@.*)", "$1$2");
雖然你可以完全避免的正則表達式,並使用String#indexOf
方法來找出2個位置,讓使用該子。
試用這個「$ 1 @ $ 2」進行更換。 –
固定它。其實我打算把'@'放在括號內,但錯誤輸入。 – anubhava
試試這個。
str = str.replaceAll("\\+[^@]*@", "@");
class Test {
public static void main(String[] args) {
String str = "bunny+12kddddd+++ddd/d/d/d/d\\####[email protected]";
str = str.replaceAll("\\+[^@]*@", "@");
System.out.println(str);
}
}
s = s.replace(s.substring(s.indexOf("+"), s.indexOf("@")), "");
沒有必要使用正則表達式。你可以這樣做只是使用一個循環:
for(;;) {
int start = str.indexOf('+');
if(start == -1) break;
int stop = str.indexOf('@');
if(stop == -1) break;
str = str.substring(0,start+1) + str.substring(stop);
}
這是更冗長,但可以更好地解釋給別人以後維護代碼什麼是你的意思做。並非每個人都很喜歡解碼正則表達式。
我同意你的觀點,正則表達式可能需要比預期更多的處理時間。這是一個更優雅的解決方案。如果您願意,可以將其更改爲單行。 str = str.substring(0,str.indexOf('+')+ 1)+ str.substring(str.indexOf('@')+1,str.length); –
@ScriptShiva是的,非常優雅,雖然我試圖拼出來,以證明目標更清楚。如果你想全部替換它們,無論如何你都需要在循環中進行某種退出檢查。 – Will
我的意思是這並不比一個小的正則表達式更好。也許你反其道而行之。 –
這是一個使用RegExp分組的解決方案。
String str = "[email protected]";
final Pattern pattern = Pattern.compile("(.+?)\\+.*@(.+?)$");
final Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1) +matcher.group(2));
乾杯
public static string RemoveSpecialCharacters(string input) {
Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
}
using System.Text.RegularExpressions; –
這個問題是關於Java而不是C#。 –
只寫一個實用工具類切片字符串:
public class MyStringUtils {
public static void main (String[] args) {
String str = "[email protected]";
int startIndex = str.indexOf('+');
int endIndex = str.indexOf('@');
System.out.println("Outer: " + sliceRangeOuter(str, startIndex, endIndex));
System.out.println("Inner: " + sliceRangeInner(str, startIndex, endIndex));
}
public static String sliceStart(String str, int startIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
return str.substring(startIndex);
}
public static String sliceEnd(String str, int endIndex) {
if (endIndex < 0)
endIndex = str.length() + endIndex;
return str.substring(0, endIndex);
}
public static String sliceRangeInner(String str, int startIndex, int endIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
if (endIndex < 0)
endIndex = str.length() + endIndex;
return str.substring(startIndex, endIndex);
}
public static String sliceRangeOuter(String str, int startIndex, int endIndex) {
if (startIndex < 0)
startIndex = str.length() + startIndex;
if (endIndex < 0)
endIndex = str.length() + endIndex;
return sliceEnd(str, startIndex) + sliceStart(str, endIndex);
}
}
輸出:
Outer: [email protected]
Inner: +12kl
- 1. 刪除字符串之間的兩個符號之間的字符串
- 2. 刪除兩個字符串之間的隨機字符串android
- 3. 刪除兩個字符串之間的匹配字符
- 4. 替換/刪除兩個字符之間的字符串
- 5. sed:刪除兩個字符串之間的字符
- 6. 刪除兩個字符串之間的通用字符
- 7. 正則表達式刪除兩個字符之間的子字符串
- 8. 在兩個分隔符之間刪除字符串
- 9. 刪除文件名中出現的兩個字符串之間的字符串
- 10. 刪除兩個字符串之間的字符串的一部分
- 11. 刪除unix中兩個單詞之間的字符串
- 12. 刪除兩個逗號之間的字符串
- 13. 刪除兩個字符串之間的文本sed,awk
- 14. 刪除兩個字符串之間的點
- 15. 刪除兩個字符串之間的引號
- 16. 如何刪除兩個字符串之間的多餘匹配?
- 17. 在一行中刪除兩個字符串之間的字符串
- 18. 結合兩個字符串,刪除重複的子字符串
- 19. 如何刪除字符串中兩個字符之間的字符?
- 20. 如何找到兩個字符串之間的子字符串?
- 21. 兩個字符串之間的所有常見子字符串
- 22. 查找兩個字符串之間的所有子字符串
- 23. 提取Haskell中兩個子字符串之間的字符串
- 24. C#:如何刪除2個字符串之間的匹配子字符串?
- 25. 兩個字符串之間
- 26. 刪除兩個字符之間的字符
- 27. 提取兩個字符之間的子字符串中的C#
- 28. RegEx模式刪除兩個特定字符之間的字符串
- 29. sed刪除不同行上兩個字符串之間的字符
- 30. 刪除兩個字符之間的最後一個字VB.Net
是那裏只是出現一次,或者你想刪除所有這些? – Will