简体   繁体   English

正则表达式C#用于〜分隔文本文件

[英]Regex C# for ~ delimited text file

I am trying to match 190 in the following ~ delimited text file 我正在尝试在以下〜分隔文本文件中匹配190

GPSE~21~ADVANCED PAVING~P.O. BOX 12847~Ogden~UT~84201~190~12/5/2008~OVER 60~2/3/2009~112458~12/5/2008~12/5/2008~5176~WESTERN GAS PROCESSOR, GRANGER~MOUNTAIN GAS PLANT~GRANGER~WY~82934~7533~TESORO REFINING~474 WEST 900 NORTH~SALT LAKE CITY~UT~841031494~BUT~Freight~5000~0.0577~288.5~360.63
GPSE~21~ADVANCED PAVING~P.O. BOX 12847~Ogden~UT~84201~190~12/5/2008~OVER 60~2/3/2009~~12/5/2008~12/5/2008~~~~~~~~~~~~~~FUEL SURCHARGE~288.5~0.25~72.13~360.63

there are basically 2 lines with number 190. I wantto use regex to match "190". 基本上有2行与数字190。我想使用正则表达式来匹配“ 190”。 I am new with regex and I dunno How i can match this. 我是正则表达式的新手,我不知道该如何匹配。 Can anyone help me with creating a regular expression to match "190" in both the lines. 谁能帮助我创建一个正则表达式以匹配两行中的“ 190”。 Thanks. 谢谢。

A regex to match "190" between ~ symbols would be: 在〜符号之间匹配“ 190”的正则表达式为:

/~190~/

If you're trying to match the eighth field in your ~ delimited file, split on ~, then take the eighth field. 如果您要匹配〜分隔文件中的第八个字段,请分割为〜,然后取第八个字段。 In Perl, for example: 例如,在Perl中:

my @fields = split /~/, $string;
my $wanted = $fields[7];

Your question is fairly ambiguous as to what you're actually trying to do. 关于您实际上要做什么,您的问题是模棱两可的。

EDIT : Ops! 编辑 :操作! Now I realized you want the regex in C#. 现在,我意识到您想要C#中的正则表达式。 Omit this message in that case. 在这种情况下,请忽略此消息。

One solution with a 'Perl' regular expression. 一种带有“ Perl”正则表达式的解决方案。 It matches any character except '~' followed by '~'. 它匹配除“〜”后跟“〜”之外的任何字符。 And that process seven times. 这个过程七次。 After this, it selects all characters until it finds the first '~' (which would be the eighth field of your file). 此后,它将选择所有字符,直到找到第一个“〜”(这将是文件的第八个字段)。 Parentheses saves that content in variable '$1'. 括号会将内容保存在变量“ $ 1”中。

/(?:[^~]*~){7}([^~]*)/

A test : 测试

Content of script.pl script.pl的内容

use warnings;
use strict;

while ( <DATA> ) {
        print qq[$1\n] if m/(?:[^~]*~){7}([^~]*)/;
}

__DATA__                                                                                                                                                                                                                                     
GPSE~21~ADVANCED PAVING~P.O. BOX 12847~Ogden~UT~84201~190~12/5/2008~OVER 60~2/3/2009~112458~12/5/2008~12/5/2008~5176~WESTERN GAS PROCESSOR, GRANGER~MOUNTAIN GAS PLANT~GRANGER~WY~82934~7533~TESORO REFINING~474 WEST 900 NORTH~SALT LAKE CITY~UT~841031494~BUT~Freight~5000~0.0577~288.5~360.63                                                                                                                                                                                          
GPSE~21~ADVANCED PAVING~P.O. BOX 12847~Ogden~UT~84201~190~12/5/2008~OVER 60~2/3/2009~~12/5/2008~12/5/2008~~~~~~~~~~~~~~FUEL SURCHARGE~288.5~0.25~72.13~360.63

Running the script: 运行脚本:

perl script.pl

And result: 结果:

190
190

Since you essentially only need to get the 8th field, you won't need regular expressions at all. 由于您基本上只需要获取第8个字段,因此根本不需要正则表达式。

This little snippet should do the trick (nicely wrapped in a method for easy usage - I even did the error handling part for you): 这个小片段应该可以解决问题(巧妙地包装在一种方法中,易于使用-我什至为您完成了错误处理部分):

public string GetInvoiceNumber(string line)
{
    if(line == null)
    {
        throw new ArgumentNullException("line");
    }

    var res = line.Split('~');

    if(res.Length < 8)
    {
        throw new ArgumentException("The given line of text does not contain an invoice number!", "line");
    }

    return res[7];
}

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

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