在字符串中查找特定句子

时间:2016-11-14 21:08:29

标签: c#

我在Winfroms工作。 我有一个非常大的字符串,我希望字符串只包含一些特定单词的句子。 我尝试使用一些方法,但它们对我没用。 有人有想法吗?

例如,我有这个字符串:

 "warnings": [
        "INFORMATION FOR THE PATIENT 10 mL Vial (1000 Units per vial) WARNINGS THIS LILLY HUMAN INSULIN PRODUCT DIFFERS FROM ANIMAL–SOURCE INSULINS BECAUSE IT IS STRUCTURALLY IDENTICAL TO THE INSULIN PRODUCED BY YOUR BODY’S PANCREAS AND BECAUSE OF ITS UNIQUE MANUFACTURING PROCESS. ANY CHANGE OF INSULIN SHOULD BE MADE CAUTIOUSLY AND ONLY UNDER MEDICAL SUPERVISION. CHANGES IN STRENGTH, MANUFACTURER, TYPE (E.G., REGULAR, NPH, ANALOG), SPECIES, OR METHOD OF MANUFACTURE MAY RESULT IN THE NEED FOR A CHANGE IN DOSAGE. SOME PATIENTS TAKING HUMULIN® (HUMAN INSULIN, rDNA ORIGIN) MAY REQUIRE A CHANGE IN DOSAGE FROM THAT USED WITH OTHER INSULINS. IF AN ADJUSTMENT IS NEEDED, IT MAY OCCUR WITH THE FIRST DOSE OR DURING THE FIRST SEVERAL WEEKS OR MONTHS. DIABETES Insulin is a hormone produced by the pancreas, a large gland that lies near the stomach. This hormone is necessary for the body’s correct use of food, especially sugar. Diabetes occurs when the pancreas does not make enough insulin to meet your body’s needs. To control your diabetes, your doctor has prescribed injections of insulin products to keep your blood glucose at a near–normal level. You have been instructed to test your blood and/or your urine regularly for glucose. Studies have shown that some chronic complications of diabetes such as eye disease, kidney disease, and nerve disease can be significantly reduced if the blood sugar is maintained as close to normal as possible. The American Diabetes Association recommends that if your pre–meal glucose levels are consistently above 130 mg/dL or your hemoglobin A1c (HbA1c) is more than 7%, you should talk to your doctor. A change in your diabetes therapy may be needed. If your blood tests consistently show below–normal glucose levels, you should also let your doctor know. Proper control of your diabetes requires close and constant cooperation with your doctor. Despite diabetes, you can lead an active and healthy life if you eat a balanced diet, exercise regularly, and take your insulin injections as prescribed by your doctor. Always keep an extra supply of insulin as well as a spare syringe and needle on hand. Always wear diabetic identification so that appropriate treatment can be given if complications occur away from home."
      ],

我试过这种方法,但它不符合我的意愿,:

public static string getBetween(string strSource, string strStart, string strEnd)
        {
            int Start=0, End=0;

            if (strSource.Contains(strStart) && strSource.Contains(strEnd))
            {

                    Start += strSource.IndexOf(strStart, 0)+strStart.Length;
                    End += strSource.IndexOf(strEnd, Start);

                return strSource.Substring(Start, End - Start);
            }
            else
            {
                return "";
            }


        }

我试图改变它,但它没有用:

public static string getBetween(string strSource, string strStart, string strEnd, string mid)
        {
            int Start=0, End=0;
            Boolean check = false;

            if (strSource.Contains(strStart) && strSource.Contains(strEnd))
            {
                while (check == false)
                {
                    Start += strSource.IndexOf(strStart, 0) + strStart.Length;
                    End += strSource.IndexOf(strEnd, Start);
                    if (strSource.Substring(Start, End - Start).Contains(mid))
                    { check = true; }
                }

                return strSource.Substring(Start, End - Start);
            }
            else
            {
                return "";
            }


        }

我想将它插入到richTextBox中,我希望只显示带有“blood”字样的句子。

由于

2 个答案:

答案 0 :(得分:1)

这是使用RegEx执行此操作的方法:

public static void Main(string[] args)
{
    string source= "INFORMATION FOR THE PATIENT 10 mL Vial (1000 Units per vial) WARNINGS THIS LILLY HUMAN INSULIN PRODUCT DIFFERS FROM ANIMAL–SOURCE INSULINS BECAUSE IT IS STRUCTURALLY IDENTICAL TO THE INSULIN PRODUCED BY YOUR BODY’S PANCREAS AND BECAUSE OF ITS UNIQUE MANUFACTURING PROCESS. ANY CHANGE OF INSULIN SHOULD BE MADE CAUTIOUSLY AND ONLY UNDER MEDICAL SUPERVISION. CHANGES IN STRENGTH, MANUFACTURER, TYPE (E.G., REGULAR, NPH, ANALOG), SPECIES, OR METHOD OF MANUFACTURE MAY RESULT IN THE NEED FOR A CHANGE IN DOSAGE. SOME PATIENTS TAKING HUMULIN® (HUMAN INSULIN, rDNA ORIGIN) MAY REQUIRE A CHANGE IN DOSAGE FROM THAT USED WITH OTHER INSULINS. IF AN ADJUSTMENT IS NEEDED, IT MAY OCCUR WITH THE FIRST DOSE OR DURING THE FIRST SEVERAL WEEKS OR MONTHS. DIABETES Insulin is a hormone produced by the pancreas, a large gland that lies near the stomach. This hormone is necessary for the body’s correct use of food, especially sugar. Diabetes occurs when the pancreas does not make enough insulin to meet your body’s needs. To control your diabetes, your doctor has prescribed injections of insulin products to keep your blood glucose at a near–normal level. You have been instructed to test your blood and/or your urine regularly for glucose. Studies have shown that some chronic complications of diabetes such as eye disease, kidney disease, and nerve disease can be significantly reduced if the blood sugar is maintained as close to normal as possible. The American Diabetes Association recommends that if your pre–meal glucose levels are consistently above 130 mg/dL or your hemoglobin A1c (HbA1c) is more than 7%, you should talk to your doctor. A change in your diabetes therapy may be needed. If your blood tests consistently show below–normal glucose levels, you should also let your doctor know. Proper control of your diabetes requires close and constant cooperation with your doctor. Despite diabetes, you can lead an active and healthy life if you eat a balanced diet, exercise regularly, and take your insulin injections as prescribed by your doctor. Always keep an extra supply of insulin as well as a spare syringe and needle on hand. Always wear diabetic identification so that appropriate treatment can be given if complications occur away from home.";
    string[] sentences = Regex.Split(temp, @"(?<!\w\.\w.)(?<!(Dr|Esq|Hon|Jr|Sr|Mr|Mrs|Ms|Messrs|Mmes|Msgr|Prof|Rev|Rt|Sr|St|Ltd|Col|Gen|Cpl|[A-Z][a-z]?)\.)(?<=\.|\?)\s");
    List<string> result = getBetween(sentences);
    myRichTextBox.Lines = result.ToArray(); // This will write to your rich text box
}
public static List<string> getBetween(string[] sentences)
{
    string key = "blood";
    List<string> results = new List<string>();
    foreach (string sentence in sentences)
    {
        if (sentence.Contains(key))
            results.Add(sentence);
    }
    return results;
}

答案 1 :(得分:0)

将字符串拆分成句子,然后遍历句子。这是一个快速的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            var strSource = "INFORMATION FOR THE PATIENT 10 mL Vial (1000 Units per vial) WARNINGS THIS LILLY HUMAN INSULIN PRODUCT DIFFERS FROM ANIMAL–SOURCE INSULINS BECAUSE IT IS STRUCTURALLY IDENTICAL TO THE INSULIN PRODUCED BY YOUR BODY’S PANCREAS AND BECAUSE OF ITS UNIQUE MANUFACTURING PROCESS. ANY CHANGE OF INSULIN SHOULD BE MADE CAUTIOUSLY AND ONLY UNDER MEDICAL SUPERVISION. CHANGES IN STRENGTH, MANUFACTURER, TYPE (E.G., REGULAR, NPH, ANALOG), SPECIES, OR METHOD OF MANUFACTURE MAY RESULT IN THE NEED FOR A CHANGE IN DOSAGE. SOME PATIENTS TAKING HUMULIN® (HUMAN INSULIN, rDNA ORIGIN) MAY REQUIRE A CHANGE IN DOSAGE FROM THAT USED WITH OTHER INSULINS. IF AN ADJUSTMENT IS NEEDED, IT MAY OCCUR WITH THE FIRST DOSE OR DURING THE FIRST SEVERAL WEEKS OR MONTHS. DIABETES Insulin is a hormone produced by the pancreas, a large gland that lies near the stomach. This hormone is necessary for the body’s correct use of food, especially sugar. Diabetes occurs when the pancreas does not make enough insulin to meet your body’s needs. To control your diabetes, your doctor has prescribed injections of insulin products to keep your blood glucose at a near–normal level. You have been instructed to test your blood and/or your urine regularly for glucose. Studies have shown that some chronic complications of diabetes such as eye disease, kidney disease, and nerve disease can be significantly reduced if the blood sugar is maintained as close to normal as possible. The American Diabetes Association recommends that if your pre–meal glucose levels are consistently above 130 mg/dL or your hemoglobin A1c (HbA1c) is more than 7%, you should talk to your doctor. A change in your diabetes therapy may be needed. If your blood tests consistently show below–normal glucose levels, you should also let your doctor know. Proper control of your diabetes requires close and constant cooperation with your doctor. Despite diabetes, you can lead an active and healthy life if you eat a balanced diet, exercise regularly, and take your insulin injections as prescribed by your doctor. Always keep an extra supply of insulin as well as a spare syringe and needle on hand. Always wear diabetic identification so that appropriate treatment can be given if complications occur away from home.";
            string[] strEnd = { ". ","? ","! " };
            var sentences = strSource.Split(strEnd,StringSplitOptions.None);
            var output = new StringBuilder();
            foreach (var sentence in sentences)
            {
                if (sentence.ToLower().Contains("blood"))
                {
                    output.AppendLine(sentence);
                }
            }
            Console.Write(output.ToString());
            Console.ReadLine();
        }
    }
}