0

I've been searching, and trying to figure out how to write a regular expression to solve my problem. So far I've come up short. This is what I'm trying to do. Find the part of a string that contains "Offset: " and extract the number after it. My issue is that the number can be from 1 to 10,000,000. Its the comma's that have me stumped. Any help is welcome. This is a sample of what it might look like. It's for c# just in case, as I read there can be different flavors of regex. Here is a sample of text.

"22:22:33.4643071","taskhostw.exe","4504","WriteFile","C:\Users\M O Shea\AppData\Local\Microsoft\Windows\WebCache\V01.log","SUCCESS","Offset: 286,720, Length: 4,096, I/O Flags: Write Through, Priority: Normal"

abdc Offset: 1 some more text
abdc Offset: 10 some more text
abdc Offset: 100 some more text
abdc Offset: 1,000 some more text
abdc Offset: 10,000 some more text
abdc Offset: 100,000 some more text
abdc Offset: 1,000,000 some more text
maccettura
  • 10,514
  • 3
  • 28
  • 35
  • `var numberWithComma = someString.Replace("Offset: ", "");` No need for regex at all – maccettura Apr 26 '18 at 21:14
  • Will this work if the Offset part is in the middle of a long string. There is also more text after the number – Mark O Shea Apr 26 '18 at 21:16
  • 1
    You should include an actual example of your input so we can give you accurate answers. I suspect Regex will be total overkill for your needs – maccettura Apr 26 '18 at 21:17
  • So there will only ever be alpha characters in the string? No special characters besides the `:` and `,`? No other numbers? – maccettura Apr 26 '18 at 21:21
  • I see your latest edit, will every input line be in the same order? Meaning `Offset` will always appear last? – maccettura Apr 26 '18 at 21:26
  • In a comma separated file yes, I had to split the file by " " in my method because of all the commas nested in the numbers – Mark O Shea Apr 26 '18 at 21:35

2 Answers2

0

try this:

offset:\s*(?<offset>\d+(,\d+)*)

The match.Groups["offset"].Value should give you your number.

Mike_G
  • 16,237
  • 14
  • 70
  • 101
  • @maccettura yeah, but it satisfies the conditions. – Mike_G Apr 26 '18 at 21:20
  • It doesn't give you the _number_, it gives you the matching _string_. You still need to parse that string to get a number, and that's slightly complicated by the commas, [but see here](https://stackoverflow.com/questions/1824326/convert-toint32-a-string-with-commas). – TypeIA Apr 26 '18 at 21:23
  • Thats perfect thank. Yes im converting it to a number later. Thanks again :-) – Mark O Shea Apr 26 '18 at 21:27
0

If I'm reading you correctly, this works

Offset:[ ]+(10{0,2}(?:,0{3})?|10?(?:,0{3}){2})(?![,\d])

https://regex101.com/r/yyhW7K/1

Readable version

 Offset: [ ]+     
 (                             # (1 start)
      1 
      0{0,2}
      (?: ,0{3} )?
   |  10?
      (?: ,0{3} ){2}
 )                             # (1 end)
 (?! [,\d] )