0

I have the following rows of data:

login;visit_homepage;buy

register;login;buy;visit_homepage

I want to match all rows that have login event followed by buy event. There might be some other events between the login and buy events. Buy can be last event and login can be first event. I need exact match of login and buy not wildcard.

I have tried this:

SELECT * FROM events WHERE events_list ~ 'login;.*buy;?.*$';

but i think it does not cover all cases.

Thanks

7
  • Have you tried anything? Commented Feb 21, 2017 at 15:33
  • include what you have tried Commented Feb 21, 2017 at 15:33
  • I have tried this: SELECT * FROM events WHERE events_list ~ 'login;.*buy;?.*$'; Commented Feb 21, 2017 at 15:36
  • include that in the question... :) Commented Feb 21, 2017 at 15:36
  • should it select both the rows..? Commented Feb 21, 2017 at 15:40

2 Answers 2

2

This regex should match the rows:

\mlogin\M.*?\mbuy\M

It searches the word login followed later by buy.

  • \m matches the beginning of a word
  • \M matches the end of a word

Usage of the boundaries prevent matching login or buy that is composing a word (ex. foologin).

More info on these POSIX escapes in PostgreSQL here.

Sign up to request clarification or add additional context in comments.

1 Comment

better than mine to avoid foologin cases. One more good practice learnt.
0

Try this regex to select both the rows

login;.*buy.*$ or login.*buy.*$

you can check the demo here

Or you can simplify it even more by using login.*buy

2 Comments

it will not work correctly because .* will match anything before and after buy.
I want to match all rows that have login event followed by buy event.. that is your requirement. my answer matches all rows containing event list having login followed by buy. i dont get where it is going wrong.. please provide more test cases on input output and the match you need

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.