gpt4 book ai didi

c# - 如何将包含编号列表的字符串拆分为多个字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:07 25 4
gpt4 key购买 nike

我正在尝试获取一个字符串,其中包含一个编号列表,每个列表旁边都有数据项,并根据每个数字旁边的数据将它们分成多个字符串。我尝试使用正则表达式,但这导致了一些问题,因为一些数据是货币值(value),例如 £120,000.00。

示例数据为

  1. 先生测试测试

  2. 100,000.00 英镑

  3. 5 测试路,测试街

测试输入:

string testInput = "1. Mr Test Test 2. £100,000 3. 5 Test Road"

但是,编号列表可能会出现在字符串中的不同行上,因为字符串是使用 PDFBox 中的 PdfTextStripperPDF 中提取的/p>

有什么方法可以准确拆分吗?

我最初是使用 indexof 并依赖下一个数据项标题作为停止点,但数据并不总是相同(15 点对一个,25 在另一个)。

想要的结果是:

string Name = "Mr Test Test";
string Money = "£100,000.00";
string Address = "5 Test Road, Test Street";

任何帮助将不胜感激

最佳答案

让我们实现一个简单的生成器;我们可以在循环中逐行查找:

代码:

private static IEnumerable<string> ParseListToLines(string value) {
int start = 0;
bool first = true;

for (int index = 1; ; ++index) {
string toFind = $"{index}.";

int next = value.IndexOf(toFind, start);

if (next < 0) {
yield return value.Substring(start);

break;
}

if (!first) // we don't return text before the 1st item
yield return value.Substring(start, next - start);

first = false;
start = next + toFind.Length;
}
}

演示:

 string testInput = "1. Mr Test Test 2. £100,000 3. 5 Test Road";

string[] lines = ParseListToLines(testInput).ToArray();

// string Name = lines[0].Trim();
// string Money = lines[1].Trim();
// string Address = lines[2].Trim();

Console.Write(string.Join(Environment.NewLine, lines));

结果:

 Mr Test Test
£100,000
5 Test Road

编辑更多演示,包含详细测试(项目内和项目外的新行;一位和两位数标记;文本 - ** -在初始 1. 标记之前):

  // Let's build multiline string...
string testInput = "**\r\n" + string.Join(Environment.NewLine, Enumerable
.Range(1, 12)
.Select(i => $"{i,2}.String #{i} {(i < 3 ? "\r\n Next Line" : "")}"));

Console.WriteLine("Initial:");
Console.WriteLine();

Console.WriteLine(testInput);

Console.WriteLine();
Console.WriteLine("Parsed:");
Console.WriteLine();

// ... and parse it into lines
string[] lines = ParseList(testInput)
.Select(line => line.Trim())
.Select((item, index) => $"line number {index} = \"{item}\"")
.ToArray();

Console.WriteLine(string.Join(Environment.NewLine, lines));

结果:

Initial:

** // This text - before initial "1." marker should be ingnored
1.String #1 // 1st item contains multiline text
Next Line // 1st item continuation
2.String #2 // Again, multiline marker
Next Line
3.String #3
4.String #4
5.String #5
6.String #6
7.String #7
8.String #8
9.String #9
10.String #10 // two digit markers: "10.", "11.", "12."
11.String #11
12.String #12

Parsed:

line number 0 = "String #1 // 1st item is multiline one
Next Line"
line number 1 = "String #2 // 2nd item is multiline as well
Next Line"
line number 2 = "String #3"
line number 3 = "String #4"
line number 4 = "String #5"
line number 5 = "String #6"
line number 6 = "String #7"
line number 7 = "String #8"
line number 8 = "String #9"
line number 9 = "String #10"
line number 10 = "String #11"
line number 11 = "String #12"

编辑 2:好吧,让我们尝试另一个测试:

string testInput = 
"1. test 5. wrong 2. It's Correct 3. OK 4. 1. 2. 3. - all wrong 5. Corect Now;";

string[] report = ParseList(testInput)
.Select(line => line.Trim())
.ToArray();

Console.Write(string.Join((Environment.NewLine, report));

结果:

test 5. wrong
It's Correct
OK
1. 2. 3. - all wrong
Corect Now;

关于c# - 如何将包含编号列表的字符串拆分为多个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55849476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com