-1

I'm looking for help with using Regex to pull certain data from a string. I'm new to Regex and still relatively new to coding. I have looked for other questions but can't find any where the initial string might have more than one piece of data like mine.

In the below, I'm looking to extract the document number and assign to the "docNumber" string.

Case will always be 10 digits long. Customer may be 4 or 5 digits long. Document will always by 7 digits long.

The issue is that my Regex expression is just capturing the first 7 digits of the Case number.

static void Main(string[] args)
    {

        String testText = "Case 1628855986 Customer 7739 Document 9153862";

        String docNumber = Regex.Match(testText, @"\d{7}").Value;

        Console.WriteLine("The reference you are looking for is: " + docNumber);

    }

Any help would be greatly appreciated!

  • `(?<=^|\s)\d{7}(?=$|\s)`, assuming that it's always encapsulated by a white-space-character. `(?<=^|[^\d])\d{7}(?=$|[^\d])`, ensures that the character before and after the 7-digit-document is not a number. The first `^` in `(?<=^|[^\d])` means start of line, separated from the negative character-set `[^\d]` by an OR-operator `|`, meaning it matches either a 7-digit-number that also marks the beginning of a line or a 7-digit-number not preceeded by a number. – Seth Jan 27 '22 at 11:59

1 Answers1

0

If the document number is always preceded by Document , you can use a zero-width positive lookbehind assertion instead of relying on the length of the number:

string docNumber = Regex.Match(testText, @"(?<=Document )\d+").Value
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thanks @Heinzi. Unfortunately it's not. That string is something that will be determined by the person sending the data into us. Sometimes they may just send us the numbers. For example "1628855986 7739 9153862". They may also be in any order so unfortunately this wouldn't work! – liamtharris Jan 27 '22 at 11:52
  • @liamtharris: Ah, ok. In that case the "fixed-length number" solutions from the linked duplicates should work. – Heinzi Jan 27 '22 at 11:56
  • Thanks again Heinz! That's got it! String docNumber = Regex.Match(testText, @"(?<!\d)\d{7}(?!\d)").Value; – liamtharris Jan 27 '22 at 12:01