[英]Use Regex in C# to insert spaces between ConcatenatedFileNamesLikeThis?
[英]Regex - Insert spaces between parentheses and alphanumeric characters in C#
我想在單詞,數字和括號之間插入空格。 例如,我有字符串
string a = "20and(2and 3)";
我想擁有
string b = "20 and ( 2 and 3 )";
我發現這種方法可以在單詞和數字之間插入空格,並且它有效。 但是當括號時它不會插入空格...
b = Regex.Replace(a, "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
請有人幫我這個嗎...? 非常感謝你。
怎么樣呢:
b = Regex.Replace(a, "[a-zA-Z]+|[0-9]+|[()]", "$0 ");
雖然這也會在末尾插入一個空格。 如果這是一個問題,您只需Trim
結果字符串:
b = Regex.Replace(a, "[a-zA-Z]+|[0-9]+|[()]", "$0 ").Trim();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.