1

I'm new to regex and found a command on a regex tutorial/test site that will allow me to search for 3 consecutive consonants. The only problem is I can't figure out how to use it with grep. Would someone help me out? I'm trying to search a word list text file using:

(?:([bcdfghjklmnpqrstvwxzy])(?!.{1,2}\1)){3}

3

The Regex pattern you have contains:

  • Non-capturing group, (?:)
  • Zero width negative lookahead, (?!)

which are only supported by grep with PCRE, Perl Compatible Regular Expression (-P)

So you need grep -P, and to capture only the matched portion, -o:

% grep -Po '(?:([bcdfghjklmnpqrstvwxzy])(?!.{1,2}\1)){3}' <<<'foobarspfb'
rsp
  • Thanks, will except answer when it allows in a few mins. Any chance you can tell me how to make it list the whole word if it finds the pattern instead of just the pattern? – Austin Oct 31 '16 at 6:02
  • @Jake You want to match whole words only? – heemayl Oct 31 '16 at 6:04
  • no I just want to list the words that contain 3 or more consonants in a row – Austin Oct 31 '16 at 6:08
  • @Jake ummmm, sorry I don't understand. It would be better if you ask this as a new question with an input chunk and your desired output from that. – heemayl Oct 31 '16 at 6:10
  • Sorry I'll try to re-explain. I have a list of words that is too big for me to manually look through. I want to print out to my terminal each word in the list that is spelled with 3 or more non-vowel characters in a row. For example: it should print 'angst' because that has 4 consonants in a row (ngst), but it shouldn't print 'paper' because that only has 1 consonant in a row. – Austin Oct 31 '16 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.