2

I have written regex and tested it online, works fine. When I test in terminal, MySQL console, it doesn't match and I get an empty set. I believe MySQL regexp syntax is somehow different but I cannot find the right way.

This is data I use:

edu.ba;
medu.ba;
edu.ba;
med.edu.ba;
edu.com;
edu.ba

I should get only edu.ba matches including; if there is some. Works fine except in actual query.

(\;+|^)\bedu.ba\b(\;+|$|\n)

Is there anything I could change to get the same results?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Medina
  • 21
  • 2
  • 1
    Maybe `(;|^)edu[.]ba(;|$|\n)` or just `(;|^)edu[.]ba(;|$)` will do. Why do you use a word boundary if the left and right hand contexts are stricter than a word boundary? You also should escape a dot if you mean a literal dot. – Wiktor Stribiżew Jul 23 '19 at 11:26
  • OMG! You are right, so simple, but I couldn't see it.. Thanks! It is working :) – Medina Jul 23 '19 at 11:27
  • \b was unnecessary and it produced the wrong result. – Medina Jul 23 '19 at 11:29

1 Answers1

3

You want to match edu.ba in between semi-colons or start/end of string. The word boundaries are redundant here (although if you want to experiment, the MySQL regex before MySQL v8 used [[:<:]] / [[:>:]] word boundaries, and in MySQL v8+, you need to use double backslashes with \b - '\\b').

Use

(;|^)edu[.]ba(;|$)

Details

  • (;|^) - ; or start of string
  • edu[.]ba - edu.ba literal string (dot inside brackets always matches a literal dot)
  • (;|$) - ; or end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563