简体   繁体   中英

If string contains 2 period in c#

I'm trying to build a Reg Expression where if the textbox string contains two periods anywhere it will execute my code. This is what I've got so far:

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

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

However, it only executes when there are two periods together and not anywhere within the string...

There is no need for regex here, just use LINQ!

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

Or for 2 or more :

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

If performance is crucial, you should use a loop. Here is a comparison of the three approaches (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);
        }
    }
}

Try it here .

And the results:

hasTwoPeriodsLinq 1280

hasTwoPeriodsLoop 54

hasTwoPeriodsRegex 74

您应该声明两个句点以及它们之间和之间的除句点以外的所有内容:

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

Try this:

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

If count == 2, you're all good.

This works according to my tests:

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

Any character zero or more times followed by a period followed by any character zero or more times followed by a period followed by any character zero or more times.

Of course, as others have pointed out, using Regex here is not the most efficient or readable way of doing it. Regex has a learning curve and future programmers may not appreciate a less-than-straightforward approach given that there are simpler alternatives.

if you want to use regex, then you can use the Regex.Matches to check the count.

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

Several people have given examples to test for exactly 2 but here's an example to test for at least 2 periods. You could actually easily modify this to test for exactly 2 if you wanted to too.

 (.*\..*){2}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM