Rickshaw
Rickshaw

Reputation: 107

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...

Upvotes: 2

Views: 2587

Answers (6)

sdgfsdh
sdgfsdh

Reputation: 37105

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

Upvotes: 10

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}

Upvotes: 1

Balaji Marimuthu
Balaji Marimuthu

Reputation: 2058

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
}

Upvotes: 1

Baldrick
Baldrick

Reputation: 11840

Try this:

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

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

Upvotes: 4

Mark Shevchenko
Mark Shevchenko

Reputation: 8207

You should declare two periods and anything except period around and between them:

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

Upvotes: 3

rory.ap
rory.ap

Reputation: 35318

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.

Upvotes: 1

Related Questions