0

I have been searching all day on Stackoverflow and many other sites, but I just can't seem to wrap my head around trying to get regex to match what I need.

Please see example below:

This is the text I'm searching.

[Date]
;Possible values: Local, Static
;Type = Local
;If using Static type, the year/month/date to set the date to
;Year = 2012
;Month = 1
;Date = 1

[Time]
;Possible values: Local, Custom, Static
Type = Static
;If using Custom type, offset from UTC in hours (can be negative as well)
Offset = 0
;If using Static type (Hour value always the same on every server start), the value (0-24) to set the Hour to
Hour = 9

What I am trying to accomplish is a lookahead and obtain only the Type = Static under the [Time] bracket. I am using C# if that helps. I have tried many many different patterns with no success.

(?<=\[Time\]\n+Type = ).* 
(?<=\[Time\].*Type =).*

That's just a few ideas that I have tried. Can someone please show me the correct way to do this with an explanation on why it is doing what its doing? Based on the comments I noticed that I should be more clear on the fact that this file is much larger then what I have shown and almost each [SETTING] contains atleast one type flag to it. Also its almost 100% sure that the user will have ;comments put into the file so I have to be able to search out that specific [SETTING] and type to make it work.

Timg
  • 227
  • 1
  • 3
  • 12
  • 1
    Is there a specific reason you need to do this with a regex? – Dweeberly Oct 16 '13 at 21:31
  • In addition to Dweeberly's question.. would you accept a non-regex version? – Simon Whitehead Oct 16 '13 at 21:36
  • [Not sure if C# supports `\K`](http://stackoverflow.com/q/13542950), but in some languages you could use `\[time\].*\KType\s*=\s*\w+` [demo](http://regex101.com/r/rY2rX9) – HamZa Oct 16 '13 at 21:40
  • I am not apposed to non regex idea and could probably do it without an issue just doing some simple FileIO but I'm trying to practice my Regex. So I would prefer it. – Timg Oct 16 '13 at 21:44
  • It looks like you're trying to read an ini file... you might be better off finding and using an existing library or code for parsing an ini file in .NET. – David Moore Oct 16 '13 at 21:44

2 Answers2

2
var val = Regex.Match(alltext, 
                      @"\[Time\].+?Type\s+=\s+([^\s]+)", 
                      RegexOptions.Singleline)
              .Groups[1].Value;

This will return Static

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Okay I have no idea how that is working and Would love someone to explain it to me but that grabs exactly what I was looking for to a tee. Please if you don't mind share an explanation with me. – Timg Oct 16 '13 at 21:53
  • Am I to understand that the .groups is produced as the first group being @"\[Time\].+?Type\s+=\s+ and the second group [1] is ([^\s]+) – Timg Oct 16 '13 at 22:16
  • The first group is text matched for the entire pattern. Group N is the N-th regex pattern enclosed in parenthesis. http://msdn.microsoft.com/en-us/library/bs2twtah.aspx#matched_subexpression – Ben T Oct 16 '13 at 23:58
0

You can try a different pattern:

(?<=Type\s*=\s*).*(?=[\r\n;]+)

I am assuming that Type = is always followed by a new line that starts with a semicolon ;.

Thanks for @SimonWhitehead and @HamZa for reminding me, I should note that this will capture both Type = lines, so you need to ignore the first match and look for the second one only.

EDIT: You can try another expression, which is not as efficient as the first one:

(?<=\[Time\][\r\n;]*[^\r\n]+[\r\n]*Type\s*=\s*).*

RegexHero Demo

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • I didn't downvote.. but I assume whoever did is not happy with the fact this captures both of them. Which shouldn't matter to the OP.. they can just grab group 2.. +1 to undo the downvote. – Simon Whitehead Oct 16 '13 at 21:39
  • @SimonWhitehead Thank you very much. I don't know what's the problem with guys today. I got two answers down voted although they solve the problem correctly *(the OPs told me saw)* and even though I asked the down voters for a reason, they just don't respond. I want some payback :) – Ibrahim Najjar Oct 16 '13 at 21:41
  • @Sniffer Just to improve your answer: **1)** you forgot `=` in `(? – HamZa Oct 16 '13 at 21:42
  • @user2005735 Check out the demo link I posted, It is working, and if it didn't work try the other pattern please. – Ibrahim Najjar Oct 16 '13 at 21:53
  • Yea not sure what I could be doing wrong here but I can't get a match in notepad++ it gives me a invalid expression and in C# I get no match at all. – Timg Oct 16 '13 at 21:59
  • @user2005735 This won't work in Notepad++, but in C# it would work, especially the second regular expression, check the demo link where you can test both of those expressions. – Ibrahim Najjar Oct 16 '13 at 22:00