在字符串中找到一个特定的句子

本文关键字:一个 句子 字符串 | 更新日期: 2023-09-27 18:18:05

我在WinForms工作。我有一个非常大的字符串,我希望这个字符串只包含一些特定单词的句子。我试过用一些方法,但对我不起作用。有人有什么想法吗?

例如,我有这个字符串:

 "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 "";
            }

        }

我只想在RickTextBox中显示含有单词blood的句子。

谢谢

在字符串中找到一个特定的句子

这是使用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;
}

将字符串分成句子,然后循环遍历这些句子。下面是一个简短的例子:

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();
        }
    }
}