如果string在c#中包含2个句点

时间:2017-01-27 14:40:25

标签: c# regex

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

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

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

但是,只有当两个句点在一起时才会执行,而不是在字符串中的任何位置......

6 个答案:

答案 0 :(得分:9)

这里不需要正则表达式,只需使用LINQ!

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

2或更多

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

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

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

答案 1 :(得分:4)

你应该宣布两个句号和除句点之间和之间的任何东西:

[^\.]*\.[^\.]*\.[^\.]*

答案 2 :(得分:3)

试试这个:

int count = source.Count(f => f == '.');

如果count == 2,你们都很好。

答案 3 :(得分:2)

这符合我的测试:

^.*\..*\..*$

任何字符零次或多次后跟一个句点,后跟任意字符零次或多次,后跟一个句点,后跟任意字符零次或多次。

当然,正如其他人所指出的那样,在这里使用Regex并不是最有效或可读的方法。 Regex有一个学习曲线,未来的程序员可能不会欣赏一种不那么简单的方法,因为有更简单的替代方案。

答案 4 :(得分:2)

如果你想使用正则表达式,那么你可以使用Regex.Matches来检查计数。

if(Regex.Matches(stringinput, @"\.").Count == 2 )
{
//perform your operation
}

答案 5 :(得分:2)

有几个人给出了测试完全 2的示例,但这里是一个测试至少 2个周期的示例。如果你愿意的话,你可以很容易地修改它来测试2。

 (.*\..*){2}