Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match beginning and end of a word with a vowel

Tags:

I'm trying with the following Regex:

/^[aeiou]\..*[aeiou]$/ 

But it's not working, I tested "abcda" and didn't match.

like image 277
Paras Singh Avatar asked Mar 05 '16 12:03

Paras Singh


People also ask

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

How do you match a word in regex?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.

How do you match upper and lower cases in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.


2 Answers

It should be just:

/^[aeiou].*[aeiou]$/ 

The extra \. you had would require the second character to be a literal dot, like in: "a.hello". But since your test case "abcda" does not include such a dot, it did not match.

Note that if you want to match upper case vowels as well, you could add the i modifier, as follows:

/^[aeiou].*[aeiou]$/i 

If your intention is to only match strings where the ending vowel is the same as the vowel at the start, then use a back-reference \1 like this:

/^([aeiou]).*\1$/i 
like image 159
trincot Avatar answered Sep 17 '22 01:09

trincot


Regex for word start and end with vowel.

^[aeiou].*[aeiou]$ 

Regex for word start and end with same vowel.

^[a].*[a]$|^[e].*[e]$|^[i].*[i]$|^[o].*[o]$|^[u].*[u]$ 
like image 41
Navin Avatar answered Sep 19 '22 01:09

Navin