2017-01-27 5 views
1

テキストボックス文字列に2つのピリオドが含まれていると、コードが実行されるReg式を作成しようとしています。文字列に2つのピリオドが含まれている場合#

+1

いくつかの有効な無効な文字列を入力できますか?それはちょうど2つの期間でなければならないでしょう。それらの間にはスペースが必要ですか?あなたが特定のものでなければ、答えにくいです。 – Equalsk

+0

'"。*?\\ .. *?\\。 "' – Gusman

+0

なぜ 'textBoxSearch.Text.Count(c => c == '。')== 2'を使用しないのですか? – stuartd

答えて

9

正規表現の必要がここにありません、ただLINQを使用!

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

それとも2以上について:

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

パフォーマンスが非常に重要である場合は、ループを使用する必要があります。ここでは3つのアプローチ(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

+2

「2以上」は「Count()> 1」にならないでしょうか? –

+0

これもうまくいくでしょうが、 'Skip'と' Any'を使う方が速いです。これは、 'Count'が' IEnumerable'全体を処理しなければならないのに対して、 'Any'は早期に終了することができるからです。 – sdgfsdh

+0

@sdgfsdhええ、これははるかに簡単な方法です、ありがとう! – Rickshaw

2

これは私のテストに応じて動作します...二つの期間は一緒にしていない任意の場所に文字列内に存在するとき

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

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

しかし、それだけで実行されます:これは私がこれまで持っているものである

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

任意の文字の後にピリオドが続き、任意の文字が0回以上続いた後にピリオドが続き、任意の文字が0回以上続きます。

もちろん、他の人が指摘しているように、ここでRegexを使用するのは、最も効率的で読みやすい方法ではありません。正規表現には学習曲線があり、将来のプログラマーは、より簡単な選択肢があるとすれば、あまり単純ではないアプローチには気づかないかもしれません。

4

あなたは周りの期間を除いて二つの期間、何を宣言し、それらの間にする必要があります

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

これを試してみてください:

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

count == 2なら、あなたはすべて良いです。

2

regexを使用する場合は、Regex.Matchesを使用して数を確認できます。

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

何人かが正確に 2が、ここでは少なくとも 2期間をテストするための例ですをテストするための例を与えています。これを実際に簡単に修正して、正確に2をテストすることもできます。

(.*\..*){2} 
関連する問題