Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, February 2007
Regular Exp


Message 1 in thread

Hi,

I want to read key,value pairs from INI file.

I am trying to use RegExp for that.
First I match section and then key=value pairs.

For section I right

	QString pattern_Section("\\[section1\\](.*)[^(\\[)]*");
	
	QString data("[section1] UserName=abc UserPassword=hfkw7559
[section2] DefaultDomain=dev.net [section3] out=file

	QRegExp rx_Section(pattern_Section);

	int pos = rx_Section.indexIn(data, 0);

	QString result = rx_Section.cap(1);

	QMessageBox::information(NULL,"Mesg",result,"ok");

With pattern "\\[section1\\](.*)[^(\\[)]*" I mean - Matched string
should start with [section1] \\[section1\\] followed by anything (.*)
untill I get next section starter with [ symbol [^(\\[)] and anything
after that *

I want to get  UserName=abc UserPassword=hfkw7559 but I am getting the
whole string after [section1] i.e. 
 UserName=abc UserPassword=hfkw7559 [section2] DefaultDomain=dev.net
[section3] out=file

Can anyone please help me with this.

Thanks,
Jaya

--
 [ signature omitted ] 

Message 2 in thread

On 2/22/07, Jaya Meghani <Jaya.Meghani@xxxxxxxxxx> wrote:
> I want to read key,value pairs from INI file.
>
> I am trying to use RegExp for that.

'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.' —Jamie Zawinski, in
comp.lang.emacs

If you're using Qt4, there's need to reinvent the wheel. QSettings can
parse arbitrary INI-format files - see
http://doc.trolltech.com/4.2/qsettings.html#QSettings-4

-- 
 [ signature omitted ] 

Message 3 in thread

> On 2/22/07, Jaya Meghani <Jaya.Meghani@xxxxxxxxxx> wrote:
> > I want to read key,value pairs from INI file.
> >
> > I am trying to use RegExp for that.
> 
> 'Some people, when confronted with a problem, think "I know, 
> I'll use regular expressions." Now they have two problems.' 
> -Jamie Zawinski, in comp.lang.emacs
> 
> If you're using Qt4, there's need to reinvent the wheel. 
> QSettings can parse arbitrary INI-format files - see
> http://doc.trolltech.com/4.2/qsettings.html#QSettings-4

Sorry I wrote INI file just to explain the kind of input I am parsing.
Actually I am getting data in the form of INI file format (that's
section and key=value)from a webservice and there is no physical file.


> 
> --
> Andrew Medico <a.medico@xxxxxxxxx>
> 

--
 [ signature omitted ] 

Message 4 in thread

On 22.02.07 16:19:40, Jaya Meghani wrote:
> > On 2/22/07, Jaya Meghani <Jaya.Meghani@xxxxxxxxxx> wrote:
> > > I want to read key,value pairs from INI file.
> > >
> > > I am trying to use RegExp for that.
> > 
> > 'Some people, when confronted with a problem, think "I know, 
> > I'll use regular expressions." Now they have two problems.' 
> > -Jamie Zawinski, in comp.lang.emacs
> > 
> > If you're using Qt4, there's need to reinvent the wheel. 
> > QSettings can parse arbitrary INI-format files - see
> > http://doc.trolltech.com/4.2/qsettings.html#QSettings-4
> 
> Sorry I wrote INI file just to explain the kind of input I am parsing.
> Actually I am getting data in the form of INI file format (that's
> section and key=value)from a webservice and there is no physical file.

Then how about storing that data in a tempfile and letting QSettings
parse that?

If thats not an option: I suggest to write a real parser, not solely
based on regexps. Regexps are fine for pattern matches, but for many
text formats they don't work.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

On 22.02.07 16:01:24, Jaya Meghani wrote:
> I want to read key,value pairs from INI file.
> 
> I am trying to use RegExp for that.
> First I match section and then key=value pairs.

Are you aware that Qt QSettings class is able to read ini-style files?

Andreas

-- 
 [ signature omitted ] 

Message 6 in thread

> On 22.02.07 16:01:24, Jaya Meghani wrote:
> > I want to read key,value pairs from INI file.
> > 
> > I am trying to use RegExp for that.
> > First I match section and then key=value pairs.
> 
> Are you aware that Qt QSettings class is able to read ini-style files?
Yes I am aware but as I wrote in my earlier reply, I am getting data of
INI file format from a webservice.
Can any one please have a look at regular expression.
Thanks
> 
> Andreas
> 
> --

--
 [ signature omitted ] 

Message 7 in thread

On 2/22/07, Jaya Meghani <Jaya.Meghani@xxxxxxxxxx> wrote:
> > On 2/22/07, Jaya Meghani <Jaya.Meghani@xxxxxxxxxx> wrote:
> > > I want to read key,value pairs from INI file.
> > >
> > > I am trying to use RegExp for that.
> >
> > 'Some people, when confronted with a problem, think "I know,
> > I'll use regular expressions." Now they have two problems.'
> > -Jamie Zawinski, in comp.lang.emacs
> >
> > If you're using Qt4, there's need to reinvent the wheel.
> > QSettings can parse arbitrary INI-format files - see
> > http://doc.trolltech.com/4.2/qsettings.html#QSettings-4
>
> Sorry I wrote INI file just to explain the kind of input I am parsing.
> Actually I am getting data in the form of INI file format (that's
> section and key=value)from a webservice and there is no physical file.

Rather than trying to parse the data stream with one big regular
expression, my approach would be to iterate over the lines with a
state machine.

If the line matches /^\[(.*)\]/, and the captured text is the name of
the desired section set a flag. If the captured text is not the name
of the desired section, unset the flag.

If the line is not a section heading, parse the setting text by using
QString::section() to get the key (section 0 using delimiter `=') and
the value (sections 1 through -1 using delimiter `=').

-- 
 [ signature omitted ]