Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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/1/oracle/10.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
Regex 正则表达式提取句子中的两个字符串_Regex - Fatal编程技术网

Regex 正则表达式提取句子中的两个字符串

Regex 正则表达式提取句子中的两个字符串,regex,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 {

我希望提取下面的两个字符串以成为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();
        }
    }
}

谢谢如何以非编程方式使用模式?我在下面测试了字符串模式,但在link上没有匹配。我将两个双引号替换为一个双引号。输入不需要双引号或反斜杠。该模式也不需要在开头和结尾使用符号和双引号。欢迎使用StackOverflow。请在参考后回顾您的问题。你需要出示一些证据,证明你曾尝试过与张贴的问题相关的问题。