[英]Regex - Insert spaces between parentheses and alphanumeric characters in C#
I want to insert spaces between words, numbers, and parentheses. 我想在单词,数字和括号之间插入空格。 For example i have the string
例如,我有字符串
string a = "20and(2and 3)";
and i want to have 我想拥有
string b = "20 and ( 2 and 3 )";
I found this method for inserting spaces between words and numbers, and it works. 我发现这种方法可以在单词和数字之间插入空格,并且它有效。 But it doesn't insert spaces when there are parentheses...
但是当括号时它不会插入空格...
b = Regex.Replace(a, "(?<=[0-9])(?=[A-Za-z])|(?<=[A-Za-z])(?=[0-9])", " ");
Can anyone help me with this please...? 请有人帮我这个吗...? Thank You very much.
非常感谢你。
How about this instead: 怎么样呢:
b = Regex.Replace(a, "[a-zA-Z]+|[0-9]+|[()]", "$0 ");
Although this will insert a space at the end as well. 虽然这也会在末尾插入一个空格。 If that is a problem, you can just
Trim
the result string: 如果这是一个问题,您只需
Trim
结果字符串:
b = Regex.Replace(a, "[a-zA-Z]+|[0-9]+|[()]", "$0 ").Trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.