PDA

View Full Version : QRegularExpression question



vycke
12th February 2015, 17:28
I want to match strings using QRegularExpression, but it seems that the RegExp doesn't like the pattern I'm using.

I have a pattern like "foo \S is a \S", which should match both "foo blah is a bar" and "foo blah is a very big boat" .... but doesn't seem to like either. I tried a simple "the \S" against "the quick fox", that's good... but "the \S fox" doesn't match "the quick fox". Is this an impossible pattern to use?

Thanks,
Vycke

ChrisW67
12th February 2015, 21:06
Are you escaping the backslash in your string literal? If not, then your expression is looking for "foo S" literally and not finding it.

vycke
12th February 2015, 21:15
I am escaping the S. (trying something, it may be the S is looking for a single char)

ChrisW67
12th February 2015, 21:26
I didn't ask about the S. The compiler will convert the string literal "foo \S" into "foo S" inside the program. If you want the regex to read "foo \S" after the compiler has done its thing then your code should read "foo \\S"

vycke
12th February 2015, 22:51
It is. I have been coding for nearly 15 years (yeouch)... this is a question about the QRegularExpression part.

Vycke

jefftee
13th February 2015, 05:42
The \S in your regex has the meaning any single non-whitespace character. Presumably you mean one or more as in:

foo \S+ is a \S+