Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL regex not replacing second word of the string

I’m trying to remove all instances of "universiti", "university", "university of", "college", and "college of" in my query but the "university of" and "college of" only replaces the first word and not second i.e. "of".

This is what I’ve tried

SELECT Name
, TRIM( REGEXP_REPLACE(Name, '(universiti|university|university\\sof|college|college\\sof)', '') ) AS re
FROM grid
Name                                                                 |re                                                                   |
---------------------------------------------------------------------+---------------------------------------------------------------------+
Australian National University                                       |Australian National                                                  |
Monash University                                                    |Monash                                                               |
University of Queensland                                             |of Queensland                                                        |
Macquarie University                                                 |Macquarie                                                            |
UNSW Sydney                                                          |UNSW Sydney                                                          |
Newcastle University                                                 |Newcastle                                                            |
University of Wollongong                                             |of Wollongong                                                        |
University of Melbourne                                              |of Melbourne                                                         |
University of Tasmania                                               |of Tasmania                                                          |
University of Adelaide                                               |of Adelaide                                                          |
James Cook University                                                |James Cook                                                           |
University of Western Australia                                      |of Western Australia                                                 |
University of Sydney                                                 |of Sydney                                                            |
Flinders University                                                  |Flinders                                                             |

How can I get rid of "of" too?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use

SELECT Name,
  TRIM( REGEXP_REPLACE(Name, '\\b(universit[yi]|college)(\\s+of\\b\\s*)?', '') ) AS re
FROM grid

See the regex demo. Details:

  • \b – a word boundary
  • (universit[yi]|college)university, universiti, college
  • (\s+of\b\s*)? – an optional sequence of
    • \s+ – one or more whitespaces
    • of – an of word
    • \b – a word boundary (so that of could not be a prefix of a longer word)
    • \s* – zero or more whitespaces.

Testing with

create table grid (Name varchar(320));
insert into grid (Name) values ("University of Queensland");
insert into grid (Name) values ("Macquarie University");
insert into grid (Name) values ("UNSW Sydney");

the output is

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading