简体   繁体   English

如何将正则表达式匹配转换为字符串 c#

[英]How to convert a Regex Match to String c#

i've made a Program that goes a big chunk of text from a calendar website, searches for tags of events and puts them into a text file.我制作了一个程序,它可以从日历网站上获取大量文本,搜索事件标签并将它们放入文本文件中。 I've added a function so you can do the query many days in the past, adn I wanted to add a search function, so you can search through the tags.我添加了一个功能,以便您可以在过去多天进行查询,我想添加一个搜索功能,以便您可以搜索标签。 I wanted to use a simple String.Contains(), but the output tags are Regex Matches, so I need to convert them.我想使用一个简单的 String.Contains(),但输出标签是正则表达式匹配,所以我需要转换它们。

My Code:我的代码:

using System;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace ESEL_Scraper
{
    class Program
    {
        static void Main(string[] args)
        {
            int queryResults = 0;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            Console.WriteLine("How many days do you want to go back?");
            String userInput = Console.ReadLine();
            Console.WriteLine("What do you want to search for?");
            String userInput2 = Console.ReadLine();
            int result = Int32.Parse(userInput);
            for(int i = 0; i < result; i++) {
            DateTime somePreviousDay = DateTime.Now.Date.AddDays(-i);
            String somePreviousDayString = somePreviousDay.ToString("dd.MM.yyyy");
            Console.WriteLine(somePreviousDayString);
            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            string site = client.DownloadString($"https://esel.at/api/termine/data?date={somePreviousDayString}&selection=false");
            String tags = "\"tags\":\"";
            String endTags = "\",";
            Regex regex = new Regex($"{tags}(.*?){endTags}");
            MatchCollection matches = regex.Matches(site);
            foreach (Match match in matches)
            {
                 if(match.Contains(userInput2)) {
                using (System.IO.StreamWriter file =
                new System.IO.StreamWriter(@"eselTags.txt", true))
                {
                    file.WriteLine(match);
                    queryResults++;
                }
            }

        }
            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);     
            Console.WriteLine($"Time taken: " + elapsedTime); 
            Console.WriteLine($"Found tags: " + queryResults); 

    }
 

}
}
}

The big chunk of text: https://esel.at/api/termine/data?date=05.09.2020&selection=false大段文字: https : //esel.at/api/termine/data?date=05.09.2020&selection=false

I hope someone can help me.我希望有一个人可以帮助我。

 String tags = "\"tags\":\"";
 String endTags = "\",";
 Regex regex = new Regex($"{tags}(.*?){endTags}");
 MatchCollection matches = regex.Matches(site);

The above code of your will only fetch string between "tags:" and "," ie, your tags property from the json string.上面的代码只会从“tags:”和“,”之间获取字符串,即从json字符串中获取tags属性。 So the matches will contains the substring from tags.所以matches将包含来自标签的子字符串。

You can create class and deserialize the big chunk of text to that class and with the help of linq you can search through tags are write the entire object into the file.您可以创建类并将大块文本反序列化为该类,并且在 linq 的帮助下,您可以搜索标签并将整个对象写入文件。

As an alternate solution I have modified your code a bit.作为替代解决方案,我稍微修改了您的代码。 please check below请检查下面

int queryResults = 0;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            Console.WriteLine("How many days do you want to go back?");
            String userInput = Console.ReadLine();
            Console.WriteLine("What do you want to search for?");
            String userInput2 = Console.ReadLine();
            int result = Int32.Parse(userInput);
            for (int i = 0; i < result; i++)
            {
                DateTime somePreviousDay = DateTime.Now.Date.AddDays(-i);
                String somePreviousDayString = somePreviousDay.ToString("dd.MM.yyyy");
                Console.WriteLine(somePreviousDayString);
                WebClient client = new WebClient();
                client.Encoding = System.Text.Encoding.UTF8;
                string site = client.DownloadString($"https://esel.at/api/termine/data?date={somePreviousDayString}&selection=false");

                var siteData = JToken.Parse(site);
                var termineList = siteData["termine"].Value<JArray>();
                var searchResults = termineList.Where(x => x.SelectToken("tags").Value<string>().Contains(userInput2));
                foreach (var searchResult in searchResults)
                {
                    string dataToWrite = JsonConvert.SerializeObject(searchResult);
                    
                        using (System.IO.StreamWriter file =
                        new System.IO.StreamWriter(@"eselTags.txt", true))
                        {
                            file.WriteLine(searchResult);
                            queryResults++;
                        }
                }
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);
                Console.WriteLine($"Time taken: " + elapsedTime);
                Console.WriteLine($"Found tags: " + queryResults);

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM