Regular Expression not working!
-
wrote on 30 Oct 2014, 10:20 last edited by
I have tried using this negative lookahead ".(?![a-z])" to split sentences based on the match - "dot, not followed by any characters in "a-z". It doesn't seem to work as this whole string is being considered as the pattern rather than the desired set. Please help!!
-
wrote on 30 Oct 2014, 11:08 last edited by
bq. Note: The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \.
http://qt-project.org/doc/qt-4.8/qregexp.html#characters-and-abbreviations-for-sets-of-characters
-
wrote on 30 Oct 2014, 12:06 last edited by
I had done that(forgot to mention here) ..still no result!!
-
wrote on 30 Oct 2014, 12:17 last edited by
Can you pass your code example with a "text to be parsed"?
-
wrote on 30 Oct 2014, 12:18 last edited by
Try
@QStringList field = str.split(".(?![a-z])");@Maybe with \.
-
wrote on 30 Oct 2014, 12:34 last edited by
This way you are getting a string comparison. If you want the .split function to use a regular expression, you must pass a QRegExp:
@QStringList field = str.split(QRegExp(".(?![a-z])"));@ -
wrote on 30 Oct 2014, 12:45 last edited by
Yes I I appreciate your help, Sirop. But, I have already tried almost everything before posting here, and unfortunately nothing works. Here's my snippet :-
QString st = "HELLO HOW ARE YOU.a THANK HOW YOU.2ELLO NOT YOU.";
QStringList ql = st.split("\.(?![a-z])");
qDebug()<< ql.at(0);OUTPUT: "HELLO HOW ARE YOU.a THANK HOW YOU.2ELLO NOT YOU"
-
wrote on 30 Oct 2014, 12:50 last edited by
jazco let me try this..
-
wrote on 30 Oct 2014, 12:55 last edited by
yeah jazzco yours works with double backslash! Thanks a ton, jazzco and sirop.
-
Hi,
Double backslashes are needed because you are still writing a string, so you need escape the backslash properly in order to obtain the correct regexp. You can test and validate regular expressions for QRegExp with the regular expression example from Qt's sources.
-
wrote on 31 Oct 2014, 04:14 last edited by
Yes. I get that.
4/11