使用分隔符从文本文件中读取字符串 [英] Reading a string from text file with delimiters

查看:98
本文介绍了使用分隔符从文本文件中读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有这样的文字文件



第1行:{'DEPT-ELECTRICAL';'用户';

第2行:'这是DMM的第一次演出。'; TIME:12:00:00’ ; '版本:6.1.2

第3行:';'F01RY0DM08';'#history';'UP8';



我需要用单引号作为单个字符串来考虑和读取内容。例如,输出应该是String1 = DEPT-ELECTRICAL,String2 = USER ...

半冒号是这里的分隔符。



我已经在网上提到了一些解决方案。我复制了一些代码用于启动,虽然它不是我需要的但是它给了我一个错误,因为foreach声明不能应用于Systems.Text.Regularexpr.match类型的变量。请帮助我



我尝试过:



  //  读取文件中的所有文字 
string sData = File.ReadAllText( @ c:/file.txt);

// 匹配字符串
匹配匹配= Regex.Match(sData, < span class =code-string> \(\\w | \\d | \\\\ | \\\)* \
RegexOptions.IgnoreCase);

// 读取结果并删除out of them
foreach var sResult in match)
{
sResult = sResult.Remove( 0 1 )。删除(sResult) .length - 2 1 );
// 使用sResult执行任何操作
}

解决方案

匹配只是一个结果。你正在寻找收集结果,匹配



 MatchCollection matches = Regex .Matches(... 
foreach (匹配匹配 matche中s){
string sResult = match.Value
...


< blockquote>我担心你的RegEx与你想要的不匹配。

尝试类似:'(。+)';



以下是RegEx文档的链接:

perlre - perldoc.perl.org [< a href =http://perldoc.perl.org/perlre.htmltarget =_ blanktitle =New Window> ^ ]

这是指向工具的链接帮助构建RegEx并调试它们:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

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

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


hi all, i have text file like this

LINE 1: {'DEPT-ELECTRICAL';'USER';
LINE 2: 'This is the first performance for DMM.'; 'TIME:12:00:00'; 'VERSION:6.1.2
LINE 3: ';'F01RY0DM08';'#history';'UP8";

I need to consider and read the contents with in single quotes as a single string. For eg., the output should be String1 = DEPT-ELECTRICAL, String2 = USER...
Semi colon is the delimiter here.

I have referred some solutions online. I copied some code for a head start eventhough it's not exactly what i require but it's giving me an error as "foreach statement cannot be applied to variables of type Systems.Text.Regularexpr.match". please help me

What I have tried:

// Read all text from file
                string sData = File.ReadAllText(@"c:/file.txt");

                // Match strings between " "
                Match match = Regex.Match(sData, "\"(\\w|\\d|\\s|\\\")*\"",
                                          RegexOptions.IgnoreCase);

                // Read results and strip " out of them
                foreach (var sResult in match)
                {
                    sResult = sResult.Remove(0, 1).Remove(sResult.length - 2, 1);
                    // Do whatever with sResult
                }

解决方案

A match is only a single result. You are looking for the collection result, "Matches"

MatchCollection matches = Regex.Matches(...
foreach (Match match in matches) {
   string sResult = match.Value
   ...


I fear your RegEx do not match what you looking for.
Try something like: "'(.+)';"

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.[^]


这篇关于使用分隔符从文本文件中读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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