-1

I am trying to extract a part of a string in C#, but am not sure how to accomplish this. I imagine Regex, but being a complete RegEx noob I figured I would ask the community for some help.

I have a string much like the one below:

Unit Code : Billing Period : Billing Date : Due Date : Total Amount Due : STATEMENT OF ACCOUNT Page No.: Page 1 of 2 July 2016 1S-D-0303 07/07/2016 JOHN DOE Unit 303D, Building name, CITY NAME

I am looking to extract the number "2" from the "Page 1 of 2" statement, as well as the "1S-D-0303" and "07/07/2016" part.

I try hope someone out there can help, as I am pretty much stuck and Googling hasn't gotten me anywhere.

theB
  • 6,450
  • 1
  • 28
  • 38
Robert Benedetto
  • 1,590
  • 2
  • 29
  • 52

3 Answers3

0

The following code matches exactly the 3 parts you are looking for:

using System.Text.RegularExpressions;

string pattern = @"Page [0-9]+ of ([0-9]+) [A-Z][a-z]+ [0-9]+ ([0-9A-Za-z-]+) ([0-9][0-9]\/[0-9][0-9]\/[0-9]+)";

Regex r = new Regex(pattern);
MatchCollection mc = r.Matches("Unit Code : Billing Period : Billing Date : Due Date : Total Amount Due : STATEMENT OF ACCOUNT Page No.: Page 1 of 2 July 2016 1S-D-0303 07/07/2016 JOHN DOE Unit 303D, Building name, CITY NAME");
0

You can use a regular expression like this (demo):

Page (?<CurrentPage>\d+) of (?<TotalPage>\d+) \w+ \w+ (?<Code>.*?) (?<Date>\d{2}\/\d{2}\/\d{4})
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

This will get the first part of your request:

Page\s1\sof\s(.)

Take a look at https://regex101.com/ which provides a regex tester and some helpful hints.

dhughes
  • 645
  • 1
  • 7
  • 19