2

Using c# vs2008 Regex

I am trying to get a number from a very long string loaded from text from a file.

In this source string there are multiple instances of the data i want to get out.
Eg. "Tax Invoice No INV1870507" may be repeated 10 times in the source String, with any unknown number of characters between each match. I want to get the number "1870507" out. The number is different for every different file I load and I need to find what the number is.

Using this pattern : (?<=Tax Invoice No[\s\r\n]+INV)(?'InvNo'[^\s\r\n]+)? I can correctly get a match on every occurance and can read the number.

But I get like 10 matches and 10 groups.

I want the Regex to short circuit at the first match and return only 1 match becasue then I have the info I need and there is no need to keep matching.

Can anyone please advise?

user229044
  • 232,980
  • 40
  • 330
  • 338
Spooky2010
  • 341
  • 1
  • 8
  • 21

1 Answers1

3

You want to use this for C#, so you can just use:

Regex regex = new Regex("(?<=Tax Invoice No[\s\r\n]+INV)(?'InvNo'[^\s\r\n]+)?");
regex.Match(myString);

Match: Searches the input string for the first occurrence of a regular expression...

That should actually do what you want.

Core_F
  • 3,372
  • 3
  • 30
  • 53