c# - 如果字符串在 C# 中包含 2 个句点

标签 c# regex

我正在尝试构建一个 Reg 表达式,如果文本框字符串在任何地方包含两个句点,它将执行我的代码。这是我到目前为止所得到的:

Regex word = new Regex("(\\.){2,}");

if (word.IsMatch(textBoxSearch.Text))
{
    //my code here to execute
}

但是,它仅在两个句点在一起且不在字符串中的任何位置时执行...

最佳答案

这里不需要regex,直接用LINQ就可以了!

myString.Count(x => x == '.') == 2

或者 2 个或更多:

myString.Where(x => x == '.').Skip(1).Any()

如果性能至关重要,则应使用循环。以下是三种方法(LINQ、循环、正则表达式)的比较:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Experiment
{
    public static class Program
    {
        static bool hasTwoPeriodsLinq(string text)
        {
            return text.Count(x => x == '.') == 2;
        }
        
        static bool hasTwoPeriodsLoop(string text)
        {
            int count = 0;
            
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '.')
                {
                    // This early break makes the loop faster than regex
                    if (count == 2)
                    {
                        return false;
                    }
                    
                    count++;
                }
            }
            
            return count == 2;
        }
        
        static Regex twoPeriodsRegex = new Regex(@"^.*\..*\..*$", RegexOptions.Compiled);
        
        static bool hasTwoPeriodsRegex(string text)
        {
            return twoPeriodsRegex.IsMatch(text);
        }
        
        public static void Main(string[] args)
        {
            var text = @"The young Princess Bolk6nskaya had 
brought some work in a gold-embroidered vel- 
vet bag. Her pretty little upper lip, on which 
a delicate dark down was just perceptible, was 
too short for her teeth, but it lifted all the more 
sweetly, and was especially charming when she 
occasionally drew it down to meet the lower 
lip. As is always the case with a thoroughly at- 
tractive woman, her defectthe shortness of 
her upperlip and her half-open mouth seemed 
to be her own special and peculiar form of 
beauty. Everyone brightened at the sight of 
this pretty young woman, so soon to become 
a mother, so full of life and health, and carry- 
ing her burden so lightly. Old men and dull 
dispirited young ones who looked at her, after 
being in her company and talking to her a 
litttle while, felt as if they too were becoming, 
like her, full of life and health. All who talked 
to her, and at each word saw her bright smile 
and the constant gleam of her white teeth, 
thought that they were in a specially amiable 
mood that day. ";
            
            const int iterations = 100000;
            
            // Warm up... 
            for (int i = 0; i < iterations; i++)
            {
                hasTwoPeriodsLinq(text);
                hasTwoPeriodsLoop(text);
                hasTwoPeriodsRegex(text);
            }
            
            var watch = System.Diagnostics.Stopwatch.StartNew();
            
            // hasTwoPeriodsLinq
            watch.Restart();
            
            for (int i = 0; i < iterations; i++)
            {
                hasTwoPeriodsLinq(text);
            }
            
            watch.Stop();
            
            Console.WriteLine("hasTwoPeriodsLinq " + watch.ElapsedMilliseconds);
            
            // hasTwoPeriodsLoop
            watch.Restart();
            
            for (int i = 0; i < iterations; i++)
            {
                hasTwoPeriodsLoop(text);
            }
            
            watch.Stop();
            
            Console.WriteLine("hasTwoPeriodsLoop " + watch.ElapsedMilliseconds);
            
            // hasTwoPeriodsRegex
            watch.Restart();
            
            for (int i = 0; i < iterations; i++)
            {
                hasTwoPeriodsRegex(text);
            }
            
            watch.Stop();
            
            Console.WriteLine("hasTwoPeriodsRegex " + watch.ElapsedMilliseconds);
        }
    }
}

试一试 here .

结果:

hasTwoPeriodsLinq 1280

hasTwoPeriodsLoop 54

hasTwoPeriodsRegex 74

关于c# - 如果字符串在 C# 中包含 2 个句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896508/

相关文章:

c# - 十进制类型转换

c# - 在 C# 中为 double 添加千位分隔符

c# - 带有控制台应用程序的 FileSystemWatcher

javascript - 正则表达式中插入符号 (^) 和美元符号 ($) 的必要性是什么?

php - 如何使用preg_match提取数据?

c# - 图像未在 asp webapp 中解析 (c#)

regex - 查找并替换换行符

java - 了解正则表达式

regex - 重命名 linux 目录中文件的最佳过程

c# - Container.DataItemIndex 在 gridview 中不起作用