如何在文本文件的行中获取特定句子? [英] How to get a specific sentence in a line of a text file?

查看:89
本文介绍了如何在文本文件的行中获取特定句子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在visual C#中,我正在实现的按钮需要读取.txt文件并检查文本文件中的每一行是否以某个字符结束,如果是,则在该行上取名称并将其打印到一个消息框。到目前为止,我已设法使条件检查行的末尾是否存在指定字符,但无法获取其中的名称,因为它位于两组数字之间。该名称就在该行的第一个字符之后,就在一组数字开始之前,因为它们是用户的ID。





这是文本文件:



 1Paulo111.111.111-11addaqwe2 
2Rambo425.433-628-43ererssd3
1Momba111.111.111-11asdsad4432
1Mauricio111.111.111-22wwcssfd2
1Saulo111.111.111-11qwe1231231





因此按钮需要检查当前行是否以'2'结尾并在行中打印名称。例如,第一行中的名称是Paulo,当它以2结尾时,Paulo将打印到消息框,就像第三行和第四行一样。否则,它会跳到下一行。然后将它打印在消息框中:Paulo,Momba,Mauricio。



我该怎么做?



我尝试过:



这是我目前在按钮内的代码:



 私人  void  button1_Click(< span class =code-keyword> object  sender,EventArgs e)
{
string line,lastchar;
// 读取文件并逐行显示。
系统。 IO.StreamReader file = new System.IO.StreamReader(@& quot; rato.txt& quot;);
while ((line = file.ReadLine())!= null
{
lastchar = line.Substring(line.Length - 1 1 );
if (lastchar ==& quot; 2& quot;)MessageBox.Show(& quot;在这里打印用户的名字& quot; );
}
file.Close();
}

解决方案

你知道怎么检查最后一个字母

来找到这个词:

- 你需要找到第二个位置后的第一个数字带字符串的字符串

-然后一旦你知道这个单词的结尾,就应该很容易提取这个单词。



您还可以使用RegEx(正则表达式)



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试程序。 JavaScript,Python和PCRE。 [ ^ ]


你必须学习:

1.如何制定正则表达式模式,Google会找到你的许多在线教程。

2.如何实现正则表达式在C#中,请询问 Regex Class(System.Text。 RegularExpressions) [ ^ ]

学习以下示例并根据您的要求进行调整:

 使用系统; 
使用 System.Text.RegularExpressions;

public class 计划
{
public static void Main()
{
const string pattern = @ (?< = \d)[a-zA-Z] +(?= \d {3} [。] \d {3} [。 ] \d {3} - + 2


string [] users = new string [ 5 ]
{
1Paulo111.111.111-11addaqwe2
2Rambo425.433-628- 43ererssd3
1Momba111.111.111-11asdsad4432
1Mauricio111.111.111-22wwcssfd2
1Saulo111.111.111-11qwe1231231
};

foreach string user 用户中)
{
匹配m = Regex.Match(用户,模式);
if (m.Success)Console.WriteLine(m.Value);
}
}
}



在网上尝试主页| .NET小提琴 [ ^ ]


In visual C#, the button I am implementing needs to read a .txt file and check if each line on the text file ends with a certain character, and if it does, it takes the name on that line and prints it to a message box. So far, I have managed to make the condition to check wheter or not the specified character exists at the end of the line, but can't get the name on it, as it is between two sets of numbers. The name is right after the first character on the line and just before a set of numbers start, as they are the ID of the user.


And this is the text file:

1Paulo111.111.111-11addaqwe2
2Rambo425.433-628-43ererssd3
1Momba111.111.111-11asdsad4432
1Mauricio111.111.111-22wwcssfd2
1Saulo111.111.111-11qwe1231231



So the button needs to check if the current line ends with '2' and prints the name in the line. The name in the first line, for example, is Paulo, and as it ends with "2", "Paulo" would be printed to the messagebox, just as the third and the fourth line. Otherwise, it would skip to the next line. It would then be printed in the messagebox: "Paulo, Momba, Mauricio."

How can I do this?

What I have tried:

This is my code so far inside the button:

private void button1_Click(object sender, EventArgs e)
    {
        string line, lastchar;
        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader(@&quot;rato.txt&quot;);
        while ((line = file.ReadLine()) != null)
        {
            lastchar= line.Substring(line.Length - 1, 1);
            if (lastchar== &quot;2&quot;) MessageBox.Show(&quot;Prints the name of the user here&quot;);
        }
        file.Close();
    }

解决方案

You know how to check last char
to find the word:
-you need to find the first digit after second place in string with a loop
-then once you know where the word ends, it should be easy to extract the word.

You can also resort to RegEx (Regular Expressions)

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


You have to learn:
1. How to formulate Regex patterns, Google will find your many online tutorials.
2. How to implement Regex in C#, ask Regex Class (System.Text.RegularExpressions)[^]
Study the following example and adapt it to your requirement:

using System;
using System.Text.RegularExpressions;

public class Program
{
	public static void Main()
	{
		const string pattern = @"(?<=\d)[a-zA-Z]+(?=\d{3}[.]\d{3}[.]\d{3}-.+2


)"; string[] users = new string[5] { "1Paulo111.111.111-11addaqwe2", "2Rambo425.433-628-43ererssd3", "1Momba111.111.111-11asdsad4432", "1Mauricio111.111.111-22wwcssfd2", "1Saulo111.111.111-11qwe1231231" }; foreach (string user in users) { Match m = Regex.Match(user, pattern); if(m.Success) Console.WriteLine(m.Value); } } }


Try it out online here Home | .NET Fiddle[^]


这篇关于如何在文本文件的行中获取特定句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆