最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#用正則表達式Regex.Matches 方法檢查字符串中重復(fù)出現(xiàn)的詞

 更新時間:2024年02月06日 15:05:04   作者:wenchm  
使用正則表達式用Regex類的Matches方法,可以檢查字符串中重復(fù)出現(xiàn)的詞,Regex.Matches方法在輸入字符串中搜索正則表達式的所有匹配項并返回所有匹配,本文給大家分享C#正則表達式檢查重復(fù)出現(xiàn)的詞,感興趣的朋友一起看看吧

        可以將正則表達式理解為描述某些規(guī)則的工具,使用正則表達式可以方便地對字符串進行查找和替換的操作。

        使用正則表達式用Regex類的Matches方法,可以檢查字符串中重復(fù)出現(xiàn)的詞。

一、Regex.Matches 方法

        在輸入字符串中搜索正則表達式的所有匹配項并返回所有匹配。

1.重載 

Matches(String, String, RegexOptions, TimeSpan)

使用指定的匹配選項和超時間隔在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

Matches(String, String, RegexOptions)

使用指定的匹配選項在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

Matches(String, Int32)

從字符串中的指定起始位置開始,在指定的輸入字符串中搜索正則表達式的所有匹配項。

Matches(String)

在指定的輸入字符串中搜索正則表達式的所有匹配項。

Matches(String, String)

在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

二、Matches(String, String, RegexOptions, TimeSpan)

        使用指定的匹配選項和超時間隔在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

1.定義

using System.Text.RegularExpressions;
public static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout);
參數(shù)
input    String
要搜索匹配項的字符串。
pattern    String
要匹配的正則表達式模式。
options    RegexOptions
枚舉值的按位組合,這些枚舉值指定用于匹配的選項。
matchTimeout    TimeSpan
超時間隔;若要指示該方法不應(yīng)超時,則為 InfiniteMatchTimeout。
返回    MatchCollection
搜索操作找到的 Match 對象的集合。 如果未找到匹配項,則此方法將返回一個空集合對象。
例外
ArgumentException
出現(xiàn)正則表達式分析錯誤。
ArgumentNullException
input 或 pattern 為 null。
ArgumentOutOfRangeException
options 不是 RegexOptions 值的有效按位組合。
- 或 -
matchTimeout 為負、零或大于 24 天左右。

2.示例

// 調(diào)用 Matches(String, String, RegexOptions, TimeSpan) 方法
// 以執(zhí)行區(qū)分大小寫的比較,該比較匹配以“es”結(jié)尾的句子中的任何單詞。
// 然后, 調(diào)用Matches(String, String, RegexOptions, TimeSpan) 方法
// 對模式與輸入字符串執(zhí)行不區(qū)分大小寫的比較。
// 在這兩種情況下,超時間隔都設(shè)置為 1 秒。
// 這兩種方法返回不同的結(jié)果。
using System.Text.RegularExpressions;
namespace _084_1
{
    public class Example
    {
        public static void Main()
        {
            string pattern = @"\b\w+es\b";
            string sentence = "NOTES: Any notes or comments are optional.";
            // 調(diào)用方法不區(qū)分大小寫
            try
            {
                foreach (Match match in Regex.Matches(sentence, pattern,
                                                      RegexOptions.None,
                                                      TimeSpan.FromSeconds(1)).Cast<Match>())
                    Console.WriteLine("Found '{0}' at position {1}",
                                      match.Value, match.Index);
            }
            catch (RegexMatchTimeoutException)
            {
                // Do Nothing: Assume that timeout represents no match.
            }
            Console.WriteLine();
            // 調(diào)用方法區(qū)分大小寫
            try
            {
                foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase).Cast<Match>())
                    Console.WriteLine("Found '{0}' at position {1}",
                                      match.Value, match.Index);
            }
            catch (RegexMatchTimeoutException) { }
        }
    }
}
// 運行結(jié)果:
/*
Found 'notes' at position 11
Found 'NOTES' at position 0
Found 'notes' at position 11
 */

        其中,正則表達式模式 \b\w+es\b 的定義:\b代表在單詞邊界處開始匹配。\w+代表匹配一個或多個單詞字符。es代表匹配單詞尾文本字符串“es”。\b代表在單詞邊界處結(jié)束匹配。

三、Matches(String, String, RegexOptions)

        使用指定的匹配選項在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

1.定義

using System.Text.RegularExpressions;
public static MatchCollection Matches (string input, string pattern, RegexOptions options);
參數(shù)
input    String
要搜索匹配項的字符串。
pattern    String
要匹配的正則表達式模式。
options    RegexOptions
枚舉值的按位組合,這些枚舉值指定用于匹配的選項。
返回
MatchCollection
搜索操作找到的 Match 對象的集合。 如果未找到匹配項,則此方法將返回一個空集合對象。
例外
ArgumentException
出現(xiàn)正則表達式分析錯誤。
ArgumentNullException
input 或 pattern 為 null。
ArgumentOutOfRangeException
options 不是 RegexOptions 值的有效按位組合。

2.示例

// 調(diào)用 Matches(String, String) 方法以標識以“es”結(jié)尾的句子中的任何單詞,
// 再調(diào)用 Matches(String, String, RegexOptions) 方法對模式與輸入字符串執(zhí)行不區(qū)分大小寫的比較。
// 這兩種方法返回不同的結(jié)果。
using System.Text.RegularExpressions;
namespace _084_2
{
    public class Example
    {
        public static void Main()
        {
            string pattern = @"\b\w+es\b";
            string sentence = "NOTES: Any notes or comments are optional.";
            // 調(diào)用方法并區(qū)別大小寫
            foreach (Match match in Regex.Matches(sentence, pattern).Cast<Match>())
                Console.WriteLine("Found '{0}' at position {1}",
                                  match.Value, match.Index);
            Console.WriteLine();
            // 調(diào)用方法并不區(qū)別大小寫
            foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase).Cast<Match>())
                Console.WriteLine("Found '{0}' at position {1}",
                                  match.Value, match.Index);
        }
    }
}
// 運行結(jié)果:
/*
Found 'notes' at position 11
Found 'NOTES' at position 0
Found 'notes' at position 11
 */ 

3.示例:用正則表達式檢查字符串中重復(fù)出現(xiàn)的詞

// 用正則表達式檢查字符串中重復(fù)出現(xiàn)的詞
using System.Text.RegularExpressions;
namespace _084
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private Label? label1;
        private Button? button1;
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(16, 31),
                Name = "label1",
                Size = new Size(43, 17),
                TabIndex = 1,
                Text = "label1"
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(151, 70),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 2,
                Text = "檢查",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 12),
                Name = "groupBox1",
                Size = new Size(380, 99),
                TabIndex = 0,
                TabStop = false,
                Text = "檢查字符串重復(fù)詞"
            };
            groupBox1.Controls.Add(label1);
            groupBox1.Controls.Add(button1);
            groupBox1.SuspendLayout();
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(404, 123);
            Controls.Add(groupBox1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "檢查字符串中重復(fù)出現(xiàn)的詞";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
            label1!.Text = "The the quick brown fox  fox jumped over the lazy dog dog.";
        }
        /// <summary>
        /// 使用正則表達式查找重復(fù)出現(xiàn)單詞的集合
        /// 如果集合中有內(nèi)容遍歷集合,獲取重復(fù)出現(xiàn)的單詞
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            MatchCollection matches = MyRegex().Matches(label1!.Text);
            if (matches.Count != 0)
            {
                //第一種等效foreach,LINQ
                foreach (var word in from Match match in matches.Cast<Match>()//Cast強制顯示轉(zhuǎn)換
                                     let word = match.Groups["word"].Value
                                     select word)
                {
                    MessageBox.Show(word.ToString(), "英文單詞");
                }
                第二種等效foreach
                //foreach (Match match in matches.Cast<Match>())
                //{
                //    string word = match.Groups["word"].Value;
                //    MessageBox.Show(word.ToString(), "英文單詞");
                //}
                第三種等效for
                //for (int i = 0; i < matches.Count; i++)
                //{
                //    Match match = matches[i];
                //    string word = match.Groups["word"].Value;
                //    MessageBox.Show(word.ToString(), "英文單詞");
                //}
            }
            else { MessageBox.Show("沒有重復(fù)的單詞"); }
        }
        [GeneratedRegex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled, "zh-CN")]
        private static partial Regex MyRegex();
    }
}

四、Matches(String, Int32)

        從字符串中的指定起始位置開始,在指定的輸入字符串中搜索正則表達式的所有匹配項。

1.定義

using System.Text.RegularExpressions;
public MatchCollection Matches (string input, int startat);
參數(shù)
input    String
要搜索匹配項的字符串。
startat    Int32
在輸入字符串中開始搜索的字符位置。
返回
MatchCollection
搜索操作找到的 Match 對象的集合。 如果未找到匹配項,則此方法將返回一個空集合對象。
例外
ArgumentNullException
input 為 null。
ArgumentOutOfRangeException
startat 小于零或大于 input 的長度。

2.示例

// 使用 Match(String) 方法查找以“es”結(jié)尾的句子中的第一個單詞,
// 然后調(diào)用 Matches(String, Int32) 方法以標識以“es”結(jié)尾的任何其他單詞。
using System.Text.RegularExpressions;
namespace _084_3
{
    public class Example
    {
        public static void Main()
        {
            string pattern = @"\b\w+es\b";
            Regex regex = new(pattern);
            string sentence = "Who writes these notes and uses our paper?";
            // Get the first match.
            Match match = regex.Match(sentence);
            if (match.Success)
            {
                Console.WriteLine("Found first 'es' in '{0}' at position {1}",
                                  match.Value, match.Index);
                // Get any additional matches.
                foreach (Match m in regex.Matches(sentence, match.Index + match.Length).Cast<Match>())
                    Console.WriteLine("Also found '{0}' at position {1}",
                                      m.Value, m.Index);
            }
        }
    }
}
// 運行結(jié)果:
/*
Found first 'es' in 'writes' at position 4
Also found 'notes' at position 17
Also found 'uses' at position 27
 */

五、Matches(String)

        在指定的輸入字符串中搜索正則表達式的所有匹配項。

using  System.Text.RegularExpressions;
public MatchCollection Matches (string input);
參數(shù)
input    String
要搜索匹配項的字符串。
返回
MatchCollection
搜索操作找到的 Match 對象的集合。 如果未找到匹配項,則此方法將返回一個空集合對象。
例外
ArgumentNullException
input 為 null。

六、Matches(String, String)

        在指定的輸入字符串中搜索指定的正則表達式的所有匹配項。

1.定義

using System.Text.RegularExpressions;
public static MatchCollection Matches (string input, string pattern);
參數(shù)
input    String
要搜索匹配項的字符串。
pattern    String
要匹配的正則表達式模式。
返回
MatchCollection
搜索操作找到的 Match 對象的集合。 如果未找到匹配項,則此方法將返回一個空集合對象。
例外
ArgumentException
出現(xiàn)正則表達式分析錯誤。
ArgumentNullException
input 或 pattern 為 null。

2.源碼 

// 使用 Matches(String, String) 方法標識以“es”結(jié)尾的句子中的任何單詞。
// 使用 Matches(String) 方法標識以“es”結(jié)尾的句子中的任何單詞。
using System.Text.RegularExpressions;
namespace _084_4
{
    public class Example
    {
        /// <summary>
        /// Matches(sentence, pattern)是靜態(tài)方法
        /// Matches(sentence)不支持靜態(tài)方法
        /// </summary>
        public static void Main()
        {
            string pattern = @"\b\w+es\b";
            string sentence = "Who writes these notes?";
            foreach (Match match in Regex.Matches(sentence, pattern).Cast<Match>())
                Console.WriteLine("Found '{0}' at position {1}",
                                  match.Value, match.Index);
            Console.WriteLine("****************************");
            Regex regex = new(pattern);
            foreach (Match match in regex.Matches(sentence).Cast<Match>())
                Console.WriteLine("Found '{0}' at position {1}",
                                  match.Value, match.Index);
        }
    }
}
// 運行結(jié)果:
/*
Found 'writes' at position 4
Found 'notes' at position 17
****************************
Found 'writes' at position 4
Found 'notes' at position 17
 */

到此這篇關(guān)于C#用正則表達式Regex.Matches 方法檢查字符串中重復(fù)出現(xiàn)的詞的文章就介紹到這了,更多相關(guān)C#正則表達式檢查重復(fù)出現(xiàn)的詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#中多線程調(diào)用方式的幾種實現(xiàn)

    C#中多線程調(diào)用方式的幾種實現(xiàn)

    本文主要介紹了C#中幾種多線程調(diào)用方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-09-09
  • 淺談C#中正則表達式的使用

    淺談C#中正則表達式的使用

    本篇文章主要介紹了C#中正則表達式的使用,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解

    C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解

    本文通過代碼給大家介紹C# 數(shù)據(jù)庫鏈接字符串加密解密工具的相關(guān)知識,實現(xiàn)思路大概是使用兩個數(shù)對連接字符串進行加密,再用這兩個數(shù)進行解密,具體詳細代碼,大家參考下本文
    2018-05-05
  • WPF利用TextBlock實現(xiàn)查找結(jié)果高亮顯示效果

    WPF利用TextBlock實現(xiàn)查找結(jié)果高亮顯示效果

    在應(yīng)用開發(fā)過程中,經(jīng)常遇到這樣的需求:通過關(guān)鍵字查找數(shù)據(jù),把帶有關(guān)鍵字的數(shù)據(jù)顯示出來,同時在結(jié)果中高亮顯示關(guān)鍵字,所以本文就來和大家介紹一下如何利用TextBlock實現(xiàn)查找結(jié)果高亮顯示效果吧
    2023-08-08
  • C# Npoi如何讀取單元格圖片并獲取所在單元格位置

    C# Npoi如何讀取單元格圖片并獲取所在單元格位置

    這篇文章主要介紹了C# Npoi如何讀取單元格圖片并獲取所在單元格位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 利用C#與PLC通信實現(xiàn)設(shè)備遠程控制與管理

    利用C#與PLC通信實現(xiàn)設(shè)備遠程控制與管理

    PLC是工業(yè)自動化中用于控制機械設(shè)備、生產(chǎn)線等的核心設(shè)備,通過與PLC的通信,我們可以實現(xiàn)設(shè)備的遠程監(jiān)控、數(shù)據(jù)采集等功能,C#作為一種現(xiàn)代化的編程語言,能夠非常方便地與PLC進行通信,本文將介紹如何利用C#與PLC進行通信,并實現(xiàn)設(shè)備的遠程控制與管理
    2025-02-02
  • C# 基于消息發(fā)布訂閱模型的示例(上)

    C# 基于消息發(fā)布訂閱模型的示例(上)

    這篇文章主要介紹了C# 基于消息發(fā)布訂閱模型的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#中winform實現(xiàn)自動觸發(fā)鼠標、鍵盤事件的方法

    C#中winform實現(xiàn)自動觸發(fā)鼠標、鍵盤事件的方法

    這篇文章主要介紹了C#中winform實現(xiàn)自動觸發(fā)鼠標、鍵盤事件的方法,是C#程序設(shè)計中非常實用的功能,需要的朋友可以參考下
    2014-08-08
  • C# ManualResetEvent使用方法詳解

    C# ManualResetEvent使用方法詳解

    這篇文章主要為大家詳細介紹了ManualResetEvent使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#遍歷指定目錄下所有文件的方法

    C#遍歷指定目錄下所有文件的方法

    這篇文章主要介紹了C#遍歷指定目錄下所有文件的方法,實例分析了C#中GetFiles()方法遍歷文件的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04

最新評論

福鼎市| 漳州市| 遂昌县| 怀化市| 昌邑市| 获嘉县| 永州市| 海阳市| 宁明县| 芦溪县| 梅河口市| 岱山县| 织金县| 张家界市| 那曲县| 永泰县| 高平市| 改则县| 磐安县| 阿克陶县| 邓州市| 介休市| 大悟县| 宁都县| 南康市| 乌海市| 外汇| 库伦旗| 和田县| 巴彦淖尔市| 昔阳县| 仙游县| 会理县| 会东县| 逊克县| 昌图县| 安阳市| 集贤县| 承德县| 涞水县| 延庆县|