I have a string as mentioned below.
我有一个如下所述的字符串。
Test[0] = Value1 Test1[0] = Value2 Test3[0] = Value3....etc.,
Test [0] = Value1 Test1 [0] = Value2 Test3 [0] = Value3 ....等,
I want to use Regular Expression to separate them as below
我想使用正则表达式将它们分开,如下所示
Test[0] = Value1
Test1[0] = Value2
Test3[0] = Value3
Can you please give me some regular expression to split them one line string into multiple lines?
你能不能给我一些正则表达式将它们分成多行?
Please let me know if you need more clarification on this question.
如果您需要对此问题进行更多说明,请与我们联系。
4 个解决方案
#1
3
Try this:
尝试这个:
String s = "Test[0] = Value1 Test1[0] = Value2 Test3[0] = Value3";
String result = Regex.Replace(s, @"(?<!=)\s(?!=)", "\r\n");
The regular expression (?<!=)\s(?!=)
is searching for a whitespace character (the \s
, maybe you want to replace this with a single space , because
\s
contains also newline characters.) that has no =
before or after it and replaces it with \r\n
.
正则表达式(?<!=)\ s(?!=)正在搜索一个空格字符(\ s,也许你想用一个空格替换它,因为\ s也包含换行符。)没有=之前或之后,用\ r \ n替换它。
This produces for your example the result you expect.
这会为您的示例生成您期望的结果。
Update:
更新:
instead of guessing around with the replacement sequence for newline, there is a nice shortcut in .net
to get the newline of the current system: Environment.NewLine
而不是猜测换行的替换顺序,在.net中有一个很好的快捷方式来获取当前系统的换行符:Environment.NewLine
so change the code to:
所以将代码更改为:
String result = Regex.Replace(s, @"(?<!=)\s(?!=)", Environment.NewLine);
#2
0
You need to be more specific in what you allow before or after the newline. What you can do if there must be numbers or letters on each side (here as opposed to an =
or ]
) of a space, is this search and replace:
您需要在换行符之前或之后更准确地说明您允许的内容。如果空间的每一侧(此处与=或]相对)必须有数字或字母,则可以执行以下操作:此搜索和替换:
s/([\d\w])\s([\d\w])/\1\n\2/gi
#3
0
regex expression:
正则表达式:
(Test[\d]*\[[\d]+\])\s*\=\s*(\w+)\s*
replace expression(only an example):
替换表达式(仅示例):
$1 = $2 + chr(13)
#4
0
var results = Regex.Matches("Test[0] = Value1 Test1[0] = Value2 Test3[0] = Value3", @"[^\s]+\s*=\s*[^\s]+")
.OfType<Match>()
.Select(item => item.Value)
.ToArray();
This will match the x = y where x and y can be anything except whitespace characters.
这将匹配x = y,其中x和y可以是除空白字符之外的任何内容。
#1
3
Try this:
尝试这个:
String s = "Test[0] = Value1 Test1[0] = Value2 Test3[0] = Value3";
String result = Regex.Replace(s, @"(?<!=)\s(?!=)", "\r\n");
The regular expression (?<!=)\s(?!=)
is searching for a whitespace character (the \s
, maybe you want to replace this with a single space , because
\s
contains also newline characters.) that has no =
before or after it and replaces it with \r\n
.
正则表达式(?<!=)\ s(?!=)正在搜索一个空格字符(\ s,也许你想用一个空格替换它,因为\ s也包含换行符。)没有=之前或之后,用\ r \ n替换它。
This produces for your example the result you expect.
这会为您的示例生成您期望的结果。
Update:
更新:
instead of guessing around with the replacement sequence for newline, there is a nice shortcut in .net
to get the newline of the current system: Environment.NewLine
而不是猜测换行的替换顺序,在.net中有一个很好的快捷方式来获取当前系统的换行符:Environment.NewLine
so change the code to:
所以将代码更改为:
String result = Regex.Replace(s, @"(?<!=)\s(?!=)", Environment.NewLine);
#2
0
You need to be more specific in what you allow before or after the newline. What you can do if there must be numbers or letters on each side (here as opposed to an =
or ]
) of a space, is this search and replace:
您需要在换行符之前或之后更准确地说明您允许的内容。如果空间的每一侧(此处与=或]相对)必须有数字或字母,则可以执行以下操作:此搜索和替换:
s/([\d\w])\s([\d\w])/\1\n\2/gi
#3
0
regex expression:
正则表达式:
(Test[\d]*\[[\d]+\])\s*\=\s*(\w+)\s*
replace expression(only an example):
替换表达式(仅示例):
$1 = $2 + chr(13)
#4
0
var results = Regex.Matches("Test[0] = Value1 Test1[0] = Value2 Test3[0] = Value3", @"[^\s]+\s*=\s*[^\s]+")
.OfType<Match>()
.Select(item => item.Value)
.ToArray();
This will match the x = y where x and y can be anything except whitespace characters.
这将匹配x = y,其中x和y可以是除空白字符之外的任何内容。