0

In the example below, I want to get the string "2023". All the code I wrote for this is below. I think it shouldn't be hard to get the string "2023" like this. What is the simplest way to get the "2023" in the given string?

const string raw = @"TAAD, Türkiye Adalet Akademisi'nin 95. Kuruluş Yıl Dönümü Armağanı, y.78, S.179, Temmuz 2023, s.108-157";
var frst = raw.Split(',').FirstOrDefault(x => x.Any(char.IsDigit) && Convert.ToInt32(new string(x.Where(char.IsDigit).Take(4).ToArray())) > 2000);
var scnd = new string(frst?.Where(char.IsDigit).Take(4).ToArray());
if (scnd.Length > 0 && Convert.ToInt32(scnd) > 2000) MessageBox.Show(scnd);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Habip Oğuz
  • 921
  • 5
  • 17
  • 2
    A regular expression that matches for `@"\b2[0-9][0-9][0-9]\b"` might be the most readable solution. Note: `\b` means "word boundary". You could replace the second word boundary with `,`, but that depends on how your other records look and whether you prefer to err on the side of false positives or on the side of false negatives. – Heinzi Oct 03 '21 at 11:27

2 Answers2

3

Try regular expressions:

var pattern = @"\b(\d{4})\b";
foreach (var match in Regex.Matches(raw, pattern))
{
  // do something with match
}
rkrahl
  • 1,159
  • 12
  • 18
3

If you want to match 4 digits greater than 2000, you can use:

\b[2-9][0-9]{3}\b(?<!2000)

In parts, the pattern matches:

  • \b A word boundary to prevent a partial match
  • [2-9] Match a digit 2-9
  • [0-9]{3} Match 3 digits 0-9
  • \b A word boundary
  • (?<!2000) Negative lookbehind, assert not 2000 directly to the left

Regex demo

Note that in C# using \d also matches digits in other languages.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Good answer! However, I did not understand the last sentence: *Note that in C# using `\d` also matches digits in other languages.*. It seems the sentence does not express correctly whatever you want to tell. – Arvind Kumar Avinash Oct 04 '21 at 10:07
  • 1
    @ArvindKumarAvinash Thanks, see [this answer](https://stackoverflow.com/a/273144/5424988) for using `\d` instead of `[0-9]` I will add it to the answer for clarity. – The fourth bird Oct 04 '21 at 10:08