wxRegex: Capturing matches in filecontent seperated by a \n Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
extreme001
I live to help wx-kind
I live to help wx-kind
Posts: 192
Joined: Fri Dec 22, 2006 9:17 am
Location: Germany
Contact:

wxRegex: Capturing matches in filecontent seperated by a \n

Post by extreme001 » Mon Aug 27, 2012 5:03 pm

Hi!

I have a file, a configfile and want to read the settings and values with a regular expression, seperated by a newline.
I always get one match over the complete string. This is the expression:

Code: Select all

wxRegEx regex(_T("(?pw)^(SaveMy\\w+|Color)=(.+)$"), wxRE_ADVANCED);
And this is file content:

Code: Select all

SaveMySetting1=Value1
SaveMySetting2=Value2
Color=red
How can i get all matches line by line (like find next) without reading the file line by line and just match the regex over the complete content?

Is it possible?


Thank you very much!
Last edited by extreme001 on Tue Aug 28, 2012 9:18 am, edited 1 time in total.

Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Re: wxRegex: Capturin matches in filecontent seperated by a

Post by Mojo » Mon Aug 27, 2012 6:13 pm

I guess wxFileConfig is more suitable http://docs.wxwidgets.org/stable/wx_wxfileconfig.html
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0

extreme001
I live to help wx-kind
I live to help wx-kind
Posts: 192
Joined: Fri Dec 22, 2006 9:17 am
Location: Germany
Contact:

Re: wxRegex: Capturin matches in filecontent seperated by a

Post by extreme001 » Mon Aug 27, 2012 7:11 pm

Hey! Thank you! I know that i can do it the way you described, but i want to know to do with wxRegEx. Any idea?

User avatar
doublemax
Moderator
Moderator
Posts: 15944
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxRegex: Capturin matches in filecontent seperated by a

Post by doublemax » Mon Aug 27, 2012 7:56 pm

What is the "(?pw)" in the beginning supposed to do? I've never seen that syntax.

This one worked for me:

Code: Select all

wxString s("SaveMySetting1=Value1\nSaveMySetting2=Value2\nColor=red\n");
wxRegEx regex(_T("^(SaveMy\\w+|Color)=(.+)$"), wxRE_ADVANCED|wxRE_NEWLINE);
while( regex.Matches(s) ) {
  wxLogMessage( wxT("%s => %s"), regex.GetMatch(s, 1), regex.GetMatch(s, 2) );
  // wxRegEx doesn't support multiple matches, so delete the last match and search again
  s.Replace(regex.GetMatch(s, 0), wxEmptyString);
}
Use the source, Luke!

extreme001
I live to help wx-kind
I live to help wx-kind
Posts: 192
Joined: Fri Dec 22, 2006 9:17 am
Location: Germany
Contact:

Re: wxRegex: Capturin matches in filecontent seperated by a

Post by extreme001 » Tue Aug 28, 2012 9:17 am

Thank you very much! That's what i was looking for. I used the wrong flags.

Have a nice day.

Post Reply