2

According to Mongo documentation it uses PCRE engine, and PCRE support \b and \w for unicode characters, but following RegEx does not match with Unicode strings, Is there any solution or alternative syntax for it?

// in Mongo console:
 db.col.find({word:/\b\pL\b/});

// in PHP
(new Mongo())->db->col->find(['word'=>new MongoRegex('/\b\pL\b/u')]);
3
  • Which one does not match? The PHP one? Both? And what is the string it is supposed to match?
    – Thilo
    Jan 11, 2013 at 7:07
  • @Thilo Both, For example consider this string: "ی ش a" that contains three match. Jan 11, 2013 at 7:09
  • Maybe you can tell us where it says that MongoDB support unicode because the page on it: docs.mongodb.org/manual/reference/operator/regex/#_S_regex does not mention that and MongoDB stores data in utf8 format so unless it converts afterwards (which it prolly doesn't) then it won't work. By the way PCRE and unicode PCRE are two different things, PCRE is Perl Compatible Regular Expressions which merely denotes the expression type that the regex operator uses in MongoDB
    – Sammaye
    Jan 11, 2013 at 8:21

1 Answer 1

1

I couldn't find any documentation on exactly which features MongoDB's PCRE implementation supports, but if it includes the \pL Unicode character class as well as look-ahead and look-behind assertions, then a Unicode-aware replacement for \b would be:

(?:(?=\pL)(?<!\pL)|(?!\pL)(?<=\pL))

Basically, (?=\pL)(?<!\pL) matches if the next character is a letter while the previous one is not, whereas (?!\pL)(?<=\pL) conversely matches if the previous character is a letter but the next one is not.

Of course, this regexp can be simplified a lot if we already know something about what the adjacent characters can be. For example, the Unicode-aware version of \b\pL+\b can be written simply as:

(?<!\pL)\pL+(?!\pL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.