c# - 从 txt 下方捕获行片段
问题描述
我正在尝试使用复制的 PDF 中的正则表达式从 txt 中的表中读取并获取特定值。
例如:
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:
在上面的文字中,我想从 UF 获得价值,即 MN 和其他当然像 Vl.Moeda
我尝试了这个正则表达式,但效果不佳:
[*\n\r\s*]UF\s *.*[^\w]
解决方案
这有点困难,而您的尝试看起来很棒。我的猜测是我们可能想要捕获UF
和 Vl.Moeda
关联的值,我们也许可以这样做,也许使用这个表达式:
\b([A-Z]{2})\b\s{2,}.*\s{2,}(.+)
我们将在其中包含 a\s{2,}
以使其他类似文本失败,然后在([A-Z]{2})
and中捕获我们想要的输出(.+)
。
演示
测试
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);
}
}
}
正则表达式电路
jex.im可视化正则表达式:
推荐阅读
- swift - 在 macOS SwiftUI TextEditor 中显示行号
- python - Python:matplotlib 的自适应 xticks
- asp.net-mvc - 4.6.1 项目不再在 https://localhost:64674/ 上运行
- python - 是否有任何 Python 方法/包可用于对传递函数矩阵执行矩阵运算?
- c++ - 使用 std::range::copy 和适配器打印 std::map
- binance - Binance - '帐户余额不足,无法执行请求的操作。'
- android - 在联接查询中更改子表时不会触发 Android 房间
- error-handling - 将错误从“try_into()”转换为自定义错误
- javascript - 我在鼠标+触摸 PC 上用手指按下鼠标(我需要触摸启动)
- c# - Windows 窗体单文件可执行文件