[Interest] format of QString

Keith Gardner kreios4004 at gmail.com
Fri Apr 4 16:53:10 CEST 2014


You need to make a capture group by putting () around the expected text.
 Once you do that, you will get a list of size 1 from capturedTexts.

QRegularExpression expression("^([cfmt]_\\d\\d\\d\\d\\d\\d\\.rcd)$");


On Fri, Apr 4, 2014 at 9:50 AM, sarah jones <qtsarah at outlook.com> wrote:

> Ok this is getting weird
> I put my regular expression into a simple test program and it worked as
> expected.
> In my 'real' application the call to 'hasMatch' returns true but a call to
> capturedTexts returns an empty list
> has anyone any idea what is going on?
>
> ------------------------------
> From: qtsarah at outlook.com
> To: andre at familiesomers.nl; interest at qt-project.org
> Date: Fri, 4 Apr 2014 14:28:42 +0100
>
> Subject: Re: [Interest] format of QString
>
> hi
> I thought I 'escaped' the dot character by using \\.
> is that not the case?
>
> ------------------------------
> Date: Fri, 4 Apr 2014 15:21:47 +0200
> From: andre at familiesomers.nl
> To: qtsarah at outlook.com; interest at qt-project.org
> Subject: Re: [Interest] format of QString
>
> sarah jones schreef op 4-4-2014 15:16:
>
> Hi
> I have tried this regular expression -
>
> QRegularExpression expression("^[cfmt]_\\d\\d\\d\\d\\d\\d\\.rcd$");
>
> if (expression.match(receivedFilename).hasMatch());
>
> this finds a match for m_000006.rcd
>
> but unfortunately it also matches m_0000006.rcd
>
> and I cannot figure out why
> I have tried this in some of the online regular expression tools and it
> seems to work there
> What have I got wrong?
>
>  A dot is not treated as a literal character . but as an operator.
> http://www.regular-expressions.info/dot.html
>
> For readabiliy, I'd not use \\d 6 times, but simply use [\\d]{6} instead.
>
> André
>
>
>  Thanks
>
> Sarah
>
> > Date: Fri, 4 Apr 2014 13:58:10 +0200
> > From: andre at familiesomers.nl
> > To: interest at qt-project.org
> > Subject: Re: [Interest] format of QString
> >
> > R. Reucher schreef op 4-4-2014 13:47:
> > > A slight modification to your proposed reg-exp:
> > >
> > > QRegExp regExp("(m|c)_([0-9]{6})\\.rcp");
> >
> > For new (Qt 5) code, I really recommend using QRegularExpression instead
> > of QRegExp where possible.
> >
> > André
> >
> > >
> > > Regards, René
> > >
> > > On Friday 04 April 2014 13:25:36 Etienne Sandré-Chardonnal wrote:
> > >> Hi sarah,
> > >>
> > >> This is untested but should work:
> > >>
> > >> QRegExp regExp("(m|c)_([0-9]{6}).rcp");
> > >> if(regExp.exactMatch(fileName))
> > >> {
> > >> //Here we know that filename matches the pattern
> > >> QString letter = regExp.cap(1); //Will be "m" or "c"
> > >> QString number = regExp.cap(2); //Will contain the six digits as a
> > >> string
> > >> }
> > >>
> > >>
> > >> Regular expressions are a vast topic, you will find a lot of
> documentation
> > >> by googling it.
> > >>
> > >>
> > >> Etienne
> > >>
> > >> 2014-04-04 13:01 GMT+02:00 André Somers <andre at familiesomers.nl><andre at familiesomers.nl>
> :
> > >>> sarah jones schreef op 4-4-2014 12:47:
> > >>> Hi
> > >>> How would you advise that I ensure a QString object is of a given
> format.
> > >>> In particular I want to check that a QString conforms to the
> following
> > >>> m_ or c_
> > >>> followed by 6 digits (0-9)
> > >>> followed by ".rcd"
> > >>>
> > >>> so m_000001.rcd matches as does c_000003.rcd
> > >>> but not m_aa00002.rcd
> > >>> etc
> > >>>
> > >>> A regular expression sounds like the right tool for that. See
> > >>> QRegularExpression (Qt5) or QRegExp (Qt4).
> > >>>
> > >>> André
> > >>>
> > >>>
> > >>> _______________________________________________
> > >>> Interest mailing list
> > >>> Interest at qt-project.org
> > >>> http://lists.qt-project.org/mailman/listinfo/interest
> > > _______________________________________________
> > > Interest mailing list
> > > Interest at qt-project.org
> > > http://lists.qt-project.org/mailman/listinfo/interest
> >
> > _______________________________________________
> > Interest mailing list
> > Interest at qt-project.org
> > http://lists.qt-project.org/mailman/listinfo/interest
>
>
>
> _______________________________________________ Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140404/668a103a/attachment.html>


More information about the Interest mailing list