Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find words start with consonants that near to each other

I am trying to write a regular expression that finds words starting with consonants that are near to each other.

Here is what I come up with:

\b[^aeiou0-9\W][a-z]+\s[^aeiou0-9\W][a-z]+\b

but the issue is that I need to check before and after word.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example:

Vowels and consonants are sounds, not letters. Depending on your accent and how thinly you slice them, there are about 20 vowels and 24 consonants.

Result:

how thinly 
you slice 

Expected result:

how thinly
thinly you 
you slice 

>Solution :

\b([^aeiou0-9\W][a-z]+)\s(?=([^aeiou0-9\W][a-z]+))

Example: https://regex101.com/r/tSblEq/1

This is based off of the rules in your pattern. Instead of matching <word><space><word> directly, it’s matching <word><space><lookahead word>. It’s then grouping the <word> and the <lookahead word> into groups 1 & 2 respectively.

Because the lookahead doesn’t consume the characters, each word is evaluated individually for a following word.

Result:

Match 1 Group 1: how
Match 1 Group 2: thinly

Match 2 Group 1: thinly
Match 2 Group 2: you

Match 3 Group 1: you
Match 3 Group 2: slice

Edit to better demonstrate matching directly vs lookahead:

Direct:

how thinly you slice
         ^

The engine is left at the end of the match. ‘thinly’ cannot be evaluated.

Using lookahead:

how thinly you slice
    ^     

Because the match only consumed "how" and the space after it, the engine is now left at the beginning of the word "thinly".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading