0

I was playing around with this challenge here: Weather Observation Station 12

And I tried submitting this answer:

SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$'

I know that this answer works (inspired by this very similar question):

SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT RLIKE '^[aeiouAEIOU]' AND CITY NOT RLIKE '[aeiouAEIOU]$'

... But I couldn't write an accepted answer in a single regular expression. Can anyone explain why?


What it searches in

As a commenter pointed out, then above-written page requires login. So for good measures sake, I'll add here that it searches amongst 500 city-names, such as (each in their own row in the database):

Kissee Mills, Loma Mar, Sandy Hook, Tipton, Arlington, Turner, Slidell, Negreet, Glencoe, Chelsea, Chignik Lagoon, Pelahatchie, Hanna City, Dorrance, Albany, Monument, Manchester, Prescott, Graettinger, Cahone, Sturgis, Upperco, Highwood, Waipahu, Bowdon, Tyler, Watkins, Republic, Millville, Aguanga, Bowdon Junction, Morenci, South El Monte 

Differences between regular expressions

And I've heard that there are some minor differences in regular expressions from one language to another (PHP, JavaScript, Perl, MySQL, Ruby, etc.). But I can't find a page that explains what those differences are. I could sit down and read the documentation for each language and cross-reference it, - but are there not a place where it has been summed up?

Addition: I come from the PHP-world, - and every time I write a regular expression in MySQL, there's a seed of doubt thinking: 'Is this one of those things that are different?'. Ideally I was looking for a place, where I could kill that doubt. Especially because it's so difficult to Google, because regular expressions are so punctuation-heavy.

Zeth
  • 2,273
  • 4
  • 43
  • 91
  • You don't need `+` after `(.*)` – Barmar Jul 07 '19 at 07:29
  • 1
    The link requires a login. Please post the requirements here. – Barmar Jul 07 '19 at 07:31
  • The other question seems to be solving a different challenge, but it's hard to tell because you didn't describe the requirements clearly. – Barmar Jul 07 '19 at 07:43
  • Ugh.. I see (regarding the login). The good thing about the page is that one can easily test their answers in there. But it searching between a bunch of city-names such as `Kissee Mills, Loma Mar, Sandy Hook, Tipton, Arlington, Turner, Slidell, Negreet, Glencoe, Chelsea, Chignik Lagoon, Pelahatchie, Hanna City, Dorrance, Albany, Monument, Manchester, Prescott, Graettinger, Cahone, Sturgis, Upperco, Highwood, Waipahu, Bowdon, Tyler, Watkins, Republic, Millville, Aguanga, Bowdon Junction, Morenci, South El Monte, ... ` – Zeth Jul 07 '19 at 07:44
  • Put the requirements in the question, not a comment. – Barmar Jul 07 '19 at 07:44
  • 1
    The issue here is not the slight difference between regex engines, but rather a doubt in your requirements. Please add some sample data directly to your question. – Tim Biegeleisen Jul 07 '19 at 07:48
  • Be specific and clear when you post a question mate, you have posted two links which are having different requirements, one of them needs `AND` and another one needs `OR` operation, update your question with proper details mate – Code Maniac Jul 07 '19 at 07:49
  • @CodeManiac - I've removed the incorrect link. Sorry for the confusion. Now, there's just the link to the HackerRank-site, where the challenge is. – Zeth Jul 07 '19 at 07:55
  • @TimBiegeleisen - I've updated my question with some additional information about, why I've added the extra part to my question (differences between regular expressions). – Zeth Jul 07 '19 at 07:56
  • You still haven't completed your question by showing us _which_ names should be matching. And you don't need to show us 500 examples; just 5-10 positive/negative cases is enough :-) – Tim Biegeleisen Jul 07 '19 at 07:57

5 Answers5

6

Use the [^aeiou] character class to represent a non vowel character:

SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[^aeiou].*[^aeiou]$';

I assume that a city name would always be at least two characters. Note that REGEXP is not case sensitive.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for weighing in! And yeah, that works! But can you explain why your regular expression works and mine don't? It looks to me as if you've just negated my version: `NOT REGEXP '^[aeiou].*[aeiou]$'` vs `REGEXP '^[^aeiou].*[^aeiou]$'` – Zeth Jul 07 '19 at 07:49
  • 2
    You said `doesn't start AND end with a vowel` ... but this would allow for example `Chelsea`, which still ends in a vowel. – Tim Biegeleisen Jul 07 '19 at 07:51
1

The Weather Observation Station 12 question asks for city which don't start with a vowel AND don't end with a vowel. By writing in single regular expression like SELECT DISTINCT CITY FROM STATION WHERE CITY NOT REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$'

cases for cities like 'Alabama' would be handled but it would still output cities like 'Lee' or 'Charlotte' which either start OR end with a vowel.

This is the reason it has to be explicitly written like SELECT DISTINCT CITY FROM STATION WHERE CITY NOT RLIKE '^[aeiouAEIOU]' AND CITY NOT RLIKE '[aeiouAEIOU]$'

Also FYI, Weather Observation Station 11 problem accepts the single regular expression because it asks for OR.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chavi
  • 11
  • 1
1

select distinct city from station where not city regexp '^[aeiou].*[aeiou]$'; This is the correct solution to that hackerrank weather problem 11.

Problem 12 asks for AND while problem 11 asks for OR.

Please try both of them.

nupur
  • 11
  • 2
1

Let's have a really easy to understand solution of this problem We are just using the lower left and lower right operators to get hold of first and last chars and after which we are checking with not in operators to guide us for getting consonants outputs there!!!

SELECT DISTINCT CITY
FROM STATION 
WHERE LOWER(RIGHT(CITY,1))
NOT IN ('a','e','i','o','u')
AND LOWER(LEFT(CITY,1))
NOT IN ('a','e','i','o','u');
0

For question 11, it asks for stations either not starting with vowels or do not end with vowels. Here is my accepted solution:

SELECT DISTINCT(CITY) FROM STATION WHERE CITY REGEXP '^[^aeiou].*|[^aeiou]$';

Burak A.
  • 74
  • 1
  • 5