テキストボックス文字列に2つのピリオドが含まれていると、コードが実行されるReg式を作成しようとしています。文字列に2つのピリオドが含まれている場合#
1
A
答えて
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
これは私のテストに応じて動作します...二つの期間は一緒にしていない任意の場所に文字列内に存在するとき
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}
関連する問題
- 1. 文章に文字列が含まれている場合
- 2. 配列に文字列が含まれていない場合
- 3. URLに文字列が含まれている場合は?
- 4. coffeescript update urlに文字列が含まれている場合
- 5. セルに文字列が含まれている場合
- 6. 文字列に別の2つの文字列が含まれています
- 7. MYSQL - 列がTHEN文字が含まれている場合... ELSE
- 8. QTP:検査文字列の配列に値が含まれている場合
- 9. 大括弧で囲まれた文字列に別の文字列が含まれていない場合
- 10. vim regexを使ってピリオドが含まれていて、特定の文字が含まれていない場合に行全体を削除する
- 11. JQuery HTML文字列にHTMLにスペースが含まれている場合のリテラルエラー
- 12. Bashの場合文字列が配列に含まれていない場合はどうなりますか?
- 13. Excel - sumifs - セルに文字列が含まれている場合の条件
- 14. フォーマット文字列に "{"が含まれている場合のString.Format例外
- 15. データにカンマが含まれている場合、コンマで区切る文字列
- 16. 文字列にPythonの文字列のリストが含まれていない場合
- 17. urlハッシュに文字列が含まれていない場合、関数
- 18. 部分文字列が文字列に含まれていない場合の正規表現一致
- 19. preg_string現在文字列が含まれている場合は全体の文字列を返します。
- 20. Logstashの設定、「文字列が含まれている場合は...」
- 21. パターンの個々の文字がすべて文字列に含まれている場合はtrueを返す
- 22. SQL - 1つのテーブルの列の文字列に結合テーブルの列の文字列が含まれている場合
- 23. 最初の文字列に2番目の文字列がすべて含まれている場合は返されます。
- 24. 単語に含まれていない文字が含まれている場合にのみ文字列に一致する正規表現
- 25. 文字が含まれていない場合、正規表現のみ一致する直前の文字列
- 26. Javaの場合、文字列をソートするには、内部に数字が含まれていれば?
- 27. PHPエラー入力が含まれている場合、入力文字列が "/" の記号が含まれている場合は、 "/"
- 28. urlに配列からの文字列が含まれていない場合に実行する
- 29. Regex - 文字列に他の文字が含まれている場合のみいくつかの文字を許可する
- 30. PHP配列内のすべての値に文字列が含まれている場合を参照
いくつかの有効な無効な文字列を入力できますか?それはちょうど2つの期間でなければならないでしょう。それらの間にはスペースが必要ですか?あなたが特定のものでなければ、答えにくいです。 – Equalsk
'"。*?\\ .. *?\\。 "' – Gusman
なぜ 'textBoxSearch.Text.Count(c => c == '。')== 2'を使用しないのですか? – stuartd