如何用另一个字符串拆分字符串
How to split string by another string
我有这个字符串(来自 EDI 数据):
ISA*ESA?ISA*ESA?
*
表示可以是任意字符,任意长度。
?
表示任意单个字符。
只有ISA
和ESA
保证不变
我需要将其分成两个字符串,如下所示:"ISA~this is date~ESA|"
和
"ISA~this is more data~ESA|"
我如何在 C# 中执行此操作?
我不能使用 string.split,因为它实际上没有分隔符。
只需使用
int x = whateverString.indexOf("?ISA"); // replace ? with the actual character here
然后只使用从 0 到那个 indexOf,indexOf 到 length 的子串。
编辑:
如果 ?不为人知,
我们可以只使用正则表达式模式和匹配器吗?
Matcher matcher = Patter.compile("ISA.*ESA").match(whateverString);
if(matcher.find()) {
matcher.find();
int x = matcher.start();
}
此处 x 将给出该匹配项的起始索引。
编辑:我误将其视为 java 一个,用于 C#
string pattern = @"ISA.*ESA";
Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = myRegex.Match(whateverString); // m is the first match
while (m.Success)
{
Console.writeLine(m.value);
m = m.NextMatch(); // more matches
}
使用像 ISA(.+?)ESA 这样的正则表达式和 select 第一组
string input = "ISA~mycontent+ESA";
Match match = Regex.Match(input, @"ISA(.+?)ESA",RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[1].Value;
}
这有点老套,但你可以做...
string x = "ISA*ESA?ISA*ESA?";
x = x.Replace("*","~"); // OR SOME OTHER DELIMITER
string[] y = x.Split('~');
并非在所有情况下都是完美的,但它可以简单地解决您的问题。
您可以拆分 "ISA" 和 "ESA",然后将这些部分重新组合在一起。
string input = "ISA~this is date~ESA|ISA~this is more data~ESA|";
string start = "ISA",
end = "ESA";
var splitedInput = input.Split(new[] { start, end }, StringSplitOptions.None);
var firstPart = $"{start}{splitedInput[1]}{end}{splitedInput[2]}";
var secondPart = $"{start}{splitedInput[3]}{end}{splitedInput[4]}";
firstPart = "ISA~this is date~ESA|"
secondPart = "ISA~this is more data~ESA|";
RegEx 可能是最好的选择。参见 this link
面具是
ISA(?<data1>.*?)ESA.ISA(?<data2>.*?)ESA.
这将为您提供 2 个包含所需数据的组
Match match = Regex.Match(input, @"ISA(?<data1>.*?)ESA.ISA(?<data2>.*?)ESA.",RegexOptions.IgnoreCase);
if (match.Success)
{
var data1 = match.Groups["data1"].Value;
var data2 = match.Groups["data2"].Value;
}
如果需要找到多个匹配项,请使用 Regex.Matches
,如果需要,请指定不同的 RegexOptions
。
而不是 "splitting" 的字符串,我会将您的问题描述为 "grouping" 的字符串。这可以使用正则表达式轻松完成:
正则表达式:^(ISA.*?(?=ESA)ESA.)(ISA.*?(?=ESA)ESA.)$
解释:
^
- 声明字符串开头的位置
(
- 开始捕获组
ISA
- 完全匹配字符串 ISA
.*?(?=ESA)
- 匹配任何字符 0 次或更多次,正面向前看
string ESA
(基本匹配任意字符,直到找到字符串ESA
)
ESA
- 完全匹配字符串 ESA
.
- 匹配任何字符
)
- 结束捕获组
- 再重复一次...
$
- 断言字符串末尾的位置
示例:
string input = "ISA~this is date~ESA|ISA~this is more data~ESA|";
Regex regex = new Regex(@"^(ISA.*?(?=ESA)ESA.)(ISA.*?(?=ESA)ESA.)$",
RegexOptions.Compiled);
Match match = regex.Match(input);
if (match.Success)
{
string firstValue = match.Groups[1].Value; // "ISA~this is date~ESA|"
string secondValue = match.Groups[2].Value; // "ISA~this is more data~ESA|"
}
问题"How to split a string by another string"有两个答案。
var matches = input.Split(new [] { "ISA" }, StringSplitOptions.RemoveEmptyEntries);
和
var matches = Regex.Split(input, "ISA").ToList();
但是,第一个删除空条目,而第二个不删除。
您可以使用 Regex.Split
来完成此操作
string splitStr = "|", inputStr = "ISA~this is date~ESA|ISA~this is more data~ESA|";
var regex = new Regex($@"(?<=ESA){Regex.Escape(splitStr)}(?=ISA)", RegexOptions.Compiled);
var items = regex.Split(inputStr);
foreach (var item in items) {
Console.WriteLine(item);
}
输出:
ISA~this is date~ESA
ISA~this is more data~ESA|
请注意,如果 ISA
和 ESA
之间的字符串与我们正在寻找的模式相同,那么您将不得不找到一些聪明的方法。
稍微解释一下正则表达式:
(?<=ESA) Look-behind assertion. This portion is not captured but still matched
(?=ISA) Look-ahead assertion. This portion is not captured but still matched
使用这些环视断言,您可以找到正确的 |
字符进行拆分
我有这个字符串(来自 EDI 数据):
ISA*ESA?ISA*ESA?
*
表示可以是任意字符,任意长度。
?
表示任意单个字符。
只有ISA
和ESA
保证不变
我需要将其分成两个字符串,如下所示:"ISA~this is date~ESA|"
和
"ISA~this is more data~ESA|"
我如何在 C# 中执行此操作?
我不能使用 string.split,因为它实际上没有分隔符。
只需使用
int x = whateverString.indexOf("?ISA"); // replace ? with the actual character here
然后只使用从 0 到那个 indexOf,indexOf 到 length 的子串。
编辑: 如果 ?不为人知, 我们可以只使用正则表达式模式和匹配器吗?
Matcher matcher = Patter.compile("ISA.*ESA").match(whateverString);
if(matcher.find()) {
matcher.find();
int x = matcher.start();
}
此处 x 将给出该匹配项的起始索引。
编辑:我误将其视为 java 一个,用于 C#
string pattern = @"ISA.*ESA";
Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = myRegex.Match(whateverString); // m is the first match
while (m.Success)
{
Console.writeLine(m.value);
m = m.NextMatch(); // more matches
}
使用像 ISA(.+?)ESA 这样的正则表达式和 select 第一组
string input = "ISA~mycontent+ESA";
Match match = Regex.Match(input, @"ISA(.+?)ESA",RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[1].Value;
}
这有点老套,但你可以做...
string x = "ISA*ESA?ISA*ESA?";
x = x.Replace("*","~"); // OR SOME OTHER DELIMITER
string[] y = x.Split('~');
并非在所有情况下都是完美的,但它可以简单地解决您的问题。
您可以拆分 "ISA" 和 "ESA",然后将这些部分重新组合在一起。
string input = "ISA~this is date~ESA|ISA~this is more data~ESA|";
string start = "ISA",
end = "ESA";
var splitedInput = input.Split(new[] { start, end }, StringSplitOptions.None);
var firstPart = $"{start}{splitedInput[1]}{end}{splitedInput[2]}";
var secondPart = $"{start}{splitedInput[3]}{end}{splitedInput[4]}";
firstPart = "ISA~this is date~ESA|"
secondPart = "ISA~this is more data~ESA|";
RegEx 可能是最好的选择。参见 this link
面具是
ISA(?<data1>.*?)ESA.ISA(?<data2>.*?)ESA.
这将为您提供 2 个包含所需数据的组
Match match = Regex.Match(input, @"ISA(?<data1>.*?)ESA.ISA(?<data2>.*?)ESA.",RegexOptions.IgnoreCase);
if (match.Success)
{
var data1 = match.Groups["data1"].Value;
var data2 = match.Groups["data2"].Value;
}
如果需要找到多个匹配项,请使用 Regex.Matches
,如果需要,请指定不同的 RegexOptions
。
而不是 "splitting" 的字符串,我会将您的问题描述为 "grouping" 的字符串。这可以使用正则表达式轻松完成:
正则表达式:^(ISA.*?(?=ESA)ESA.)(ISA.*?(?=ESA)ESA.)$
解释:
^
- 声明字符串开头的位置(
- 开始捕获组ISA
- 完全匹配字符串ISA
.*?(?=ESA)
- 匹配任何字符 0 次或更多次,正面向前看 stringESA
(基本匹配任意字符,直到找到字符串ESA
)ESA
- 完全匹配字符串ESA
.
- 匹配任何字符)
- 结束捕获组- 再重复一次...
$
- 断言字符串末尾的位置
示例:
string input = "ISA~this is date~ESA|ISA~this is more data~ESA|";
Regex regex = new Regex(@"^(ISA.*?(?=ESA)ESA.)(ISA.*?(?=ESA)ESA.)$",
RegexOptions.Compiled);
Match match = regex.Match(input);
if (match.Success)
{
string firstValue = match.Groups[1].Value; // "ISA~this is date~ESA|"
string secondValue = match.Groups[2].Value; // "ISA~this is more data~ESA|"
}
问题"How to split a string by another string"有两个答案。
var matches = input.Split(new [] { "ISA" }, StringSplitOptions.RemoveEmptyEntries);
和
var matches = Regex.Split(input, "ISA").ToList();
但是,第一个删除空条目,而第二个不删除。
您可以使用 Regex.Split
来完成此操作
string splitStr = "|", inputStr = "ISA~this is date~ESA|ISA~this is more data~ESA|";
var regex = new Regex($@"(?<=ESA){Regex.Escape(splitStr)}(?=ISA)", RegexOptions.Compiled);
var items = regex.Split(inputStr);
foreach (var item in items) {
Console.WriteLine(item);
}
输出:
ISA~this is date~ESA
ISA~this is more data~ESA|
请注意,如果 ISA
和 ESA
之间的字符串与我们正在寻找的模式相同,那么您将不得不找到一些聪明的方法。
稍微解释一下正则表达式:
(?<=ESA) Look-behind assertion. This portion is not captured but still matched
(?=ISA) Look-ahead assertion. This portion is not captured but still matched
使用这些环视断言,您可以找到正确的 |
字符进行拆分