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# 如何使用regex提取文本框输入中引号和单个单词之间的单词_C#_Regex - Fatal编程技术网

C# 如何使用regex提取文本框输入中引号和单个单词之间的单词

C# 如何使用regex提取文本框输入中引号和单个单词之间的单词,c#,regex,C#,Regex,问题: 所以我有一个文本框,在其中我用引号输入标题的各个部分,比如Lord of,以及没有引号的单个单词,比如Dance。我想把它们全部提取出来,去掉引号,并将它们拆分成某种列表,这样我就可以生成SQL命令,下面显示的部分取决于一些单选按钮的选择 反正 到目前为止,我已经尝试: 这将提取引号之间的所有标题 然而,我怎样才能在没有引号的列表中找到舞蹈呢?或者如果有人输入->舞蹈之王朋友怎么办 我想把《舞之王》列入名单,没有朋友,因为它缺少一个引号…我认为这是更好的完成任务的方法。我在工作中也做过类

问题:

所以我有一个文本框,在其中我用引号输入标题的各个部分,比如Lord of,以及没有引号的单个单词,比如Dance。我想把它们全部提取出来,去掉引号,并将它们拆分成某种列表,这样我就可以生成SQL命令,下面显示的部分取决于一些单选按钮的选择

反正

到目前为止,我已经尝试:

这将提取引号之间的所有标题

然而,我怎样才能在没有引号的列表中找到舞蹈呢?或者如果有人输入->舞蹈之王朋友怎么办


我想把《舞之王》列入名单,没有朋友,因为它缺少一个引号…

我认为这是更好的完成任务的方法。我在工作中也做过类似的项目,它做得非常好

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 = "\"Legend of Tarzan\" \"Lord of Dance\"";
            string pattern = "\"(?'phase'[^\"]+)\"";
            MatchCollection matches = Regex.Matches(input, pattern);
            List<string> wheres = new List<string>();
            foreach (Match match in matches)
            {
                wheres.Add(string.Format("title % '{0}'", match.Groups["phase"].Value));
            }

            string whereClause = string.Format("WHERE {0}", string.Join(" OR ", wheres));

            string SQL = string.Format(
                " SELECT movie_id, ts_headline, title, ts_rank, rank" +
                " FROM movies" +
                " {0}" +
                " ORDER BY rank DESC",
                whereClause);

        }
    }
}

我看不出这与SQL有什么关系,所以我删除了那个标记。我也看不到与您发布的代码和问题有任何关系。。。发布示例输入、预期输出和您到目前为止尝试过的内容。@GordonLinoff是的,是真的,我不好,怎么做?我想把文本框中正在搜索的标题和单词的部分提取到一个列表中…这样我就可以用它做一些事情…我发布的代码部分是我的尝试,在这篇文章中,我只提取了引号之间的内容……但这不会提取单词Dance,例如,它周围没有引号。你问的是如何提取引号和单个单词之间的单词,但发布了一些不相关的代码。为了得到更好的答案,请详细解释你的问题。例如,我有这个文本,我想从中创建这个列表,我试着这样做,但是我忘记了复制两行代码
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 = "\"Legend of Tarzan\" \"Lord of Dance\"";
            string pattern = "\"(?'phase'[^\"]+)\"";
            MatchCollection matches = Regex.Matches(input, pattern);
            List<string> wheres = new List<string>();
            foreach (Match match in matches)
            {
                wheres.Add(string.Format("title % '{0}'", match.Groups["phase"].Value));
            }

            string whereClause = string.Format("WHERE {0}", string.Join(" OR ", wheres));

            string SQL = string.Format(
                " SELECT movie_id, ts_headline, title, ts_rank, rank" +
                " FROM movies" +
                " {0}" +
                " ORDER BY rank DESC",
                whereClause);

        }
    }
}