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.
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.
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.
[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 .
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.
\b(?:[^a\s]*a){3}[^a\s]*\b
Try this.See demo.
https://regex101.com/r/sJ9gM7/10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With