0

I want to do a regex search with SQLAlchemy like so:

db.session.query(Users).filter(User.name.op('regexp')(r'\bJohn\b')).all()

However although I have users in my MySQL database with the name 'John', I get no results returned.

If I remove \b from either side of John in the regex query then I get the result with users such as John Smith or just John. However the issue with having no \b on either side would mean people with Johnson in their name would also appear.

Why does using \b not return results? Thanks.

2
  • try ^John$ instead of word boundaries Commented Oct 20, 2015 at 17:25
  • @PruthviRaj But if a user has a word before or after 'John' it would not return. Commented Oct 20, 2015 at 17:27

1 Answer 1

1

This appears to be an issue with the way MySQL uses regex. Instead of using \b for word boundaries, [[:<:]] and [[:>:]] should be used like so:

db.session.query(Users).filter(User.name.op('regexp')(r'[[:<:]]John[[:>:]]')).all()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.