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.

5
  • 1
    Is there a specific reason you need to do this with a regex? Commented Oct 16, 2013 at 21:31
  • In addition to Dweeberly's question.. would you accept a non-regex version? Commented Oct 16, 2013 at 21:36
  • Not sure if C# supports \K, but in some languages you could use \[time\].*\KType\s*=\s*\w+ demo Commented Oct 16, 2013 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. Commented Oct 16, 2013 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. Commented Oct 16, 2013 at 21:44

2 Answers 2

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

This will return Static

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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]+)
The first group is text matched for the entire pattern. Group N is the N-th regex pattern enclosed in parenthesis. msdn.microsoft.com/en-us/library/…
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

6 Comments

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.
@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 :)
@Sniffer Just to improve your answer: 1) you forgot = in (?<Type 2) It will match "type" under [Date] too, while the OP asked specifically for [Time]
@user2005735 Check out the demo link I posted, It is working, and if it didn't work try the other pattern please.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.