2

I am trying to remove trailing letters from Building Addresses e.g. 111A.

I can find those patterns with:

Select Address from Table where Address Regexp '^[0-9]{1,6}[A-Z] '

However I am unsure how to or if I can include the regexp inside a Replace so that I don't have to do 10*26 different replaces e.g.

Update Table set Address=Replace(Address,'0A','0') where Address like '%0A %'

vs some sort of (which I've tried):

Update Table set Address=Replace(Address,regexp ([0-9]{1,6})[A-Z],\1) 
where Address Regexp '^[0-9]{1,6}[A-Z] '
1
  • You can find some regex replace function if you search, but it is very limited and useless. I forgot about doing this in MySQL and created a csv which I was parsing with regex in C# and created a new csv file and imported it in MySQL. Commented May 28, 2016 at 15:52

2 Answers 2

1

can I include the regexp inside a Replace?

No, not in MySQL. Sigh. Missing feature.

There's an extension to do this, but to install it you need to be able to control your MySQL server. Here: https://github.com/mysqludf/lib_mysqludf_preg#readme

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

2 Comments

Thanks that looks like a project and a half though! I posted my temporary solution as an answer for now, the consensus seems to be no regex replace. Lots of little things in MySql I'd change as I start to get to using it more and more...
regexp_replace is available in MariaDB.
1

I have yet to find Regex Replacement but this seemed to work for now. I neglected to mention that these were full Building Address so e.g. `111A Washington St'. This is what I ended up doing:

Update Table set Address=
concat(
left(SUBSTRING_INDEX(Address,' ',1),CHAR_LENGTH(SUBSTRING_INDEX(Address,' ',1))-1),
right(Address,(CHAR_LENGTH(Address)-CHAR_LENGTH(SUBSTRING_INDEX(Address,' ',1))))
)
where Address Regexp '^[0-9]{1,6}[A=Z] '

After spending time on similar challenges like this in this project, my conclusion is that, until and unless regex is integrated into functions such as update,locate,replace that using substring_index and char_length can get you a long way. Please see my similar solution to a follow-up question to this one here: Replace using Locate and/.or regex?

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.