Norse
Norse

Reputation: 5757

MySQL REGEXP Match two spaces or nothing

Stringy: 5/5
Stringy : 3 / 5
3 / 5  Stringy

I'm trying to match two leading spaces on a string, or ignore it if not found. I've tried:

SELECT *
FROM testtable
WHERE `file` REGEXP '(  )Stringy(: | : )'

This doesn't return any rows though

Is it possible to do this?

Upvotes: 0

Views: 566

Answers (1)

Lorenz Lo Sauer
Lorenz Lo Sauer

Reputation: 24710

You need to escape characters which have special meaning in regular expression pattern such as the brackets (, ).

Example with two leading spaces:

SELECT "  Stringy: 3/5" REGEXP '[ ]{2}Stringy(: | : )';
## 1
SELECT "  Stringy : 3 / 5" REGEXP '[ ]{2}Stringy(: | : )';
## 1

and a maximum of two leading spaces:

SELECT "Stringy : 3 / 5" REGEXP '[ ]{0,2}Stringy(: | : )';
## 1

See also the

Upvotes: 1

Related Questions