Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从下面的文本中捕获行片段_C#_Regex - Fatal编程技术网

C# 从下面的文本中捕获行片段

C# 从下面的文本中捕获行片段,c#,regex,C#,Regex,我试图从一个复制的PDF文件中,用正则表达式从txt表格中读取并获取特定值 例如: 在上面的文本中,我想从UF得到值,也就是MN,当然还有其他类似Vl.Moeda的值 我尝试了这个正则表达式,但效果不太好: [*\n\r\s*]UF\s *.*[^\w] 这有点困难,而你的尝试看起来很棒。我的猜测是,我们可能希望捕获UF和Vl.Moeda以及相关的值,我们可能能够这样做,可能使用以下表达式: \b([A-Z]{2})\b\s{2,}.*\s{2,}(.+) 其中我们将包含一个\s{2,}以使

我试图从一个复制的PDF文件中,用正则表达式从txt表格中读取并获取特定值

例如:

在上面的文本中,我想从UF得到值,也就是MN,当然还有其他类似Vl.Moeda的值

我尝试了这个正则表达式,但效果不太好:

[*\n\r\s*]UF\s *.*[^\w]

这有点困难,而你的尝试看起来很棒。我的猜测是,我们可能希望捕获
UF
Vl.Moeda
以及相关的值,我们可能能够这样做,可能使用以下表达式:

\b([A-Z]{2})\b\s{2,}.*\s{2,}(.+)
其中我们将包含一个
\s{2,}
以使其他类似文本失败,然后在
([a-Z]{2})
(.+)
中捕获我们想要的输出

试验 正则表达式电路 可视化正则表达式:


您到底希望得到什么结果?这还不清楚。线条是否总是如您所示,以破折号(-)开始,然后是数字?
\b([A-Z]{2})\b\s{2,}.*\s{2,}(.+)
using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"\b([A-Z]{2})\b\s{2,}.*\s{2,}(.+)";
        string input = @"DADOS DO FABRICANTE
* CNPJ/CPF           UF    Quantidade Peso Líquido(kg)   Vl.Moeda
- 99.999.999/9999-99 MN    4,00000    212,00000          250.400,00
Obs:
- 99.999.999/9999-99 AB    4,00000    212,00000          250.400,00000
Obs:
- 99.999.999/9999-99 XZ    4,00000    212,00000          250.400,00000
Obs:";
        RegexOptions options = RegexOptions.Multiline;
        
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}