Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match words which contain *N* times a certain letter

Tags:

regex

matlab

I am trying to match words which contain N times a letter (with regexp of MATLAB) and by this I do not mean finding only repeated letters, which I could do it like this:

\w*(\w)\1\w*

A simple example would be to find the following regular expression: Match words which contain 3 times the letter a. If the given string is:

hallo banana alabama oklahoma canaan

then the matched words should be:

banana and canaan. All the others contain less or more a.

Any help is appreciated Thank you.

like image 332
ginos Avatar asked Mar 26 '15 15:03

ginos


People also ask

How do I match a specific character in regex?

There is a method for matching specific characters using regular expressions, by defining them inside square brackets. For example, the pattern [abc] will only match a single a, b, or c letter and nothing else.

What is a ZA Z?

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.

What does the regular expression A to Z match?

[A-z] will match ASCII characters in the range from A to z , while [a-zA-Z] will match ASCII characters in the range from A to Z and in the range from a to z .

How do I find a word in a regular expression?

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.


1 Answers

\b(?:[^a\s]*a){3}[^a\s]*\b

Try this.See demo.

https://regex101.com/r/sJ9gM7/10

like image 195
vks Avatar answered Oct 02 '22 21:10

vks