regular expression – Combine multiple greps with regex

Using Raku (formerly known as Perl_6)

raku -ne '.put if .grep( m:i/ horse / | / <digit>+ <ws> replies / | / <digit>+ <alpha>+ <digit>+ / );'

Sample Input:

10 replies
Big Horse
123sdf562
replies
1354654
Fat Cat
2 replies
White Horse
Big Cat
Little Dog
5 replies
725vgfvjgh123
Black Horse
Brown Cow
8798jgjh

Sample Output:

10 replies
Big Horse
123sdf562
2 replies
White Horse
5 replies
725vgfvjgh123
Black Horse

Here’s a solution using Raku, a member of the Perl-family of programming languages. The Raku (a.k.a. Perl6) effort started in year 2000, and includes a major revision to PCRE (Regular Expressions), which some people find more readable.

The code above uses Raku’s grep command, which can take multiple m/…/ Regex matchers, in this case connected by a | Boolean OR. Note the first matcher will allow case-insensitive matching of horse because it uses the :i case-insensitive “adverb”. If you only want Horse to match, then change accordingly (and delete the :i adverb).

Of course it pays to be cautious when writing out Regexes, and you should note that not only will m/ Horse / match Big Horse, Black Horse, and White Horse, but Horse-faced as well. You may only want lines containing Horse when preceded by another word with intervening whitespace, in which case m/ <alpha>+ <ws> Horse / may fit the bill.

docs.raku.org/language/regexes
raku.org

Read more here: Source link