繁体   English   中英

从txt文件中提取字符串

[英]Extracting string from a txt file

我有一个txt文件,其中包含一些我想提取的值:例如"YOUR NUMBER:"之后的"YOUR NUMBER:"

"YOUR NUMBER:"号码"YOUR NUMBER:"没有空格,由第一组3位数字“-”和#位数字组成

---***---
i.e.:
[some text here]

YOUR NUMBER:
123-12345678

[some other text here]
---***---

我开始研究RegEx ,但是说实话,我的编码技能很差,所以我寻求帮助。 我需要提取并保存在excel,SQL或类似文件中。

有什么帮助吗?

正则表达式:您的电话号码:\\ n。*?\\ n
您可以在http://regexpal.com/上尝试RegExs

这是一个非正则表达式的方法:

string[] lines = File.ReadAllLines(path);
List<string> numbers = new List<string>();
for (int i = 0; i < lines.Length - 1; i++)
{
    string line = lines[i];
    if (line.Trim().Equals("YOUR NUMBER:", StringComparison.InvariantCultureIgnoreCase))
    {
        string number = lines[i + 1].Trim();
        string[] bothNums = number.Split('-');
        if (bothNums.Length == 2 && bothNums[0].Length == 3 && bothNums.All(n => n.All(Char.IsDigit)))
            numbers.Add(number);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM