-1

Question- select all city from station table where start and end letter of city must be any vowel (a/e/i/o/u).

The solution which I through: `` SELECT city FROM station WHERE REGEXP_LIKE (city, '^(a|e|i|o|u)$', 'i')```

But this is not Giving any output, I searched for the solution online and here is the working code: `` SELECT city FROM station WHERE REGEXP_LIKE (city, '^(a|e|i|o|u).*(a|e|i|o|u)$', 'i')```

I tried to search online but didn't understood why applying .* in between worked. For my code I think its not working as I am using same sub expression for start(^) and end($).

Can anyone please help me understand the .* working here.

  • `'^(a|e|i|o|u).*(a|e|i|o|u)$'` will only match values which are longer than one character. If someone names a city "A" then you will not match it with that regular expression. – MT0 Jul 21 '22 at 09:01

2 Answers2

0
  • . means any character
  • * means zero or more occurrences of that "any character"

More info in documentation.

So, it looks for strings that start ^ with a vowel, followed by no other characters (or maybe followed by many other characters), and ends with a vowel again.

Littlefoot
  • 131,892
  • 15
  • 35
  • 57
0

The . represents any character, and the following * says you accept any number of occurrences of it, including zero occurrences. So the combination works like the single * in MS DOS or like % in a SQL LIKE pattern.

'^(a|e|i|o|u)$'

wants the beginning of a string, one vowel, then the end of the string. So 'a' matches, but 'aa' and 'aba' don't.

'^(a|e|i|o|u).*(a|e|i|o|u)$'

wants the beginning of a string, one vowel, optionally any characters, then a vowel, then the end of the string. So 'a' matches, and so do 'aa' (vowel, zero characters, vowel), and 'aba' (vowel, one character, vowel), and 'abcdefghijklmnopqrstu' (vowel, several characters, vowel).

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73