Sajan
Sajan

Reputation: 1923

Ruby Regular expression not matching properly

I am trying to creat a RegEx to find words that contains any vowel. so far i have tried this

/(.*?\S[aeiou].*?[\s|\.])/i

but i have not used RegEx much so its not working properly. for example if i input "test is 1234 and sky fly test1234" it should match test , is, and, test1234 but showing test, is,1234 and if put something else then different output.

Upvotes: 0

Views: 56

Answers (4)

emartinelli
emartinelli

Reputation: 1047

It will solve:

\b\w*[aeiou]+\w*\b

https://www.debuggex.com/r/O-fU394iC5ErcSs7


or you can substitute \w by \S

\b\S*[aeiou]+\S*\b

https://www.debuggex.com/r/RNE6Y6q1q5yPJbe-


\b - a word boundary

\w - same as [_a-zA-Z0-9]

\S - a non-whitespace character

Upvotes: 1

shivam
shivam

Reputation: 16506

Alternatively you can also do something like:

"test is 1234 and sky fly test1234".split.find_all { |a| a =~ /[aeiou]/ }
# => ["test", "is", "and", "test1234"]

Upvotes: 2

yoavmatchulsky
yoavmatchulsky

Reputation: 2960

Try this:

\b\w*[aeiou]\w*\b

\b denotes a word boundry, so this regexp matches word bounty, zero or more letters, a vowel, zero or more letters and another word boundry

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

You could use the below regex.

\S*[aeiou]\S*

\S* matches zero or more non-space characters.

or

\w*[aeiou]\w*

Upvotes: 2

Related Questions