0

I have a MySQL table with some addresses on it. I would like to find the addresses that contain double letters between the 5th and 11th position. There is a test table with some data for reference.

CREATE TABLE test(
address VARCHAR(500));

INSERT INTO test (address)
VALUES ('Western Marrow'),
('Kennffort Plains'),
('Oaksbrook Terrace'),
('Southern Old Saybrook')

The last record 'Southern Old Saybrook' should not appear because its double letter is not within the 5th and 11th position. Now, I have used this query to get the double letters:

SELECT address
from test
WHERE address REGEXP 'a{2}|b{2}|c{2}|d{2}|e{2}|f{2}|g{2}|h{2}|i{2}|j{2}|k{2}|l{2}|m{2}|n{2}|o{2}|p{2}|q{2}|r{2}|s{2}|t{2}|u{2}|v{2}|w{2}|x{2}|y{2}|z{2}';

But I am failing to conduct the search within the given range. How could I modify the query to apply the regular expression within the range?

2
  • 1
    What about the third sentence? 'Oaksbrook Terrace' Should match? Commented Aug 18, 2021 at 19:38
  • Yes it should match because the letter contains two o within the range. Commented Aug 18, 2021 at 20:38

2 Answers 2

3

Use SUBSTR() to get the substring between 5th and 11th position.

WHERE SUBSTR(address, 5, 6) REGEXP '...'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. it works! I mark this as solved.
1

Much shorter than the hastily accepted answer:

WHERE SUBSTR(address, 5, 6) REGEXP '([a-z])\\1'

Caveat: Only checked on MySQL 8.0.

Caveat: Case folding or not is determined by collation of the string.

2 Comments

Much shorter code. Thank you. But I don't know what the \\1 means. I will look into it.
\\1 means to use what was found by the first ("1") match ((...)). In your case -- same letter twice.

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.