
S Nagre
std::string escapeChar = "\\" ; std::string bChar = "b"; std::string dotChar = ".";
std::string findWordInStr = escapeChar + bChar + dotChar + escapeChar + bChar;
This ends up with the expression "\\b.\\b", which will only ever match a single character with word break on either side (so, in your example, it should match all and only the spaces):
"Hello World and Google" ^ ^ ^
Closer would be "\\b.+?\\b", but that would still match on your spaces:
"Hello World and Google" ^ ^^ ^^ ^^
If you really want words, you are best off deciding what constitutes a
word, and then writing the regex for exactly that purpose. There is
the built-in "\\w" character class, but only you can decide whether
things like apostrophes and hyphens break words. (And that's just in
English; I have no idea what constitutes word-break most other
languages!) For English, I'd consider something like "[\\w'-]+"
(which should be: all word chars, plus apostrophes, plus hyphens).
And from a personal taste point of view, I'd likely write it exactly
that way. (I do sometimes decompose my regexes, but only if they have
repeated subsections that could better be described as a variable
name.)
You also had a small logic error, when you wrote this:
OffSetMap[foundPos] = foundLen;
"foundPos" is relative to the start of the last search, not to the
start of the whole string.
Here's my version:
| #include <map>
| #include <string>
|
| #include