Example Words: a, akkka, akokaa, kokoko, kakao, oooaooa, kkako, kakaoa
I need the regexp witch gives words with 2 or less 'a' but not the words without 'a'
Result: a, akka, kakao, oooaooa, kkako
Ok actually I am using:
SELECT word FROM dictionary_gr WHERE word REGEXP 'λ{2,3}' LIMIT 0 , 30
this returns 0 lines there are words with 2 λ's and 3 λ's
MySQL CHAR_LENGTH() returns the length (how many characters are there) of a given string. The function simply counts the number characters and ignore whether the character(s) are single-byte or multi-byte.
First, we split the string by spaces in a. Then, take a variable count = 0 and in every true condition we increment the count by 1. Now run a loop at 0 to length of string and check if our string is equal to the word.
To identify the number of words, we need to count the number of characters in a string and count the characters without the spaces and new lines. When we subtract the two, we get the word count. Let's look at an example. The query will return the number of words in each content row as wordcount .
select * from table where LENGTH(name) - LENGTH(REPLACE(name, 'a', '')) between 1 and 2
Updated to use between.
I don't know what MySQL supports in terms of lookaround assertions, but the following will do the trick:
^(?=.*a.*a?.*)(?!.*a.*a.*a.*).*$
We have a lookahead assertion that matches 1 or 2 a
characters in the string. Then we have a negative lookahead that disregards 3 or more a
s anywhere in the string. Then the final pattern just matches the whole string, providing the first two assertions are satisfied.
If MySQL doesn't support lookarounds, then @Woot4Moo's answer would be the way to go.
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