regex - 正则表达式在句子中提取两个字符串
问题描述
我希望提取下面的两个字符串以成为 ABC-DEF 。什么是一个好的正则表达式来做到这一点?
位置之前的数据包 =“ABC” 位置之后的数据包 =“DEF”
解决方案
尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "data-package-before-location=\"ABC\" data-package-after-location=\"DEF\"";
string pattern = @"(?'key'[\w-]+)=""(?'value'\w+)""";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches.Cast<Match>())
{
Console.WriteLine("Key : '{0}', Value : '{1}'", match.Groups["key"].Value, match.Groups["value"].Value);
}
Console.ReadLine();
}
}
}
推荐阅读
- python - Python 中的多变量 Kruskall Wallis 包
- python - Visual Studio Code , PS(需要解释以及如何删除它)
- c++ - 使用 gstreamer 管道剪切部分视频
- python - 我错过了什么?Python 套接字问题
- rust - 是否可以将某些结构保存为字节序列?
- sql - 如何替换此查询中的 WM_CONCAT 函数?[ORA-00904:“WM_CONCAT”:无效标识符]
- javascript - 是为每篇文章创建一个文件还是为每篇文章重复使用一个文件更好?
- apache-kafka - 孤立的 Kafka 连接任务
- html - 字幕项目重叠
- programming-languages - 线程安全和原子之间的真正区别是什么?