#1.Define a variable holding a multiline String
read -r -d '' poem <<EOD
Lily-like, white as snow,
she hardly knew
she was a woman, so
sweetly she grew.
EOD
#2.OR condition example in regex
# use egrep, awk, sed respectively
echo "$poem" |egrep '^Lily|woman'
echo "$poem" |awk '/^Lily|woman/'
echo "$poem" |sed -n '/^Lily\|woman/p'
echo "$poem" |sed -e '/^Lily/b' -e '/woman/b' -e d
#3.AND condition example in regex
# use egrep, awk, sed respectively
echo "$poem" |egrep '^she.*woman'
echo "$poem" |awk '/^she.*woman/'
echo "$poem" |awk '/^she/ && /woman/'
echo "$poem" |sed '/^she/!d; /woman/!d'
#4.NOT condition example in regex
# use egrep, awk, sed respectively
echo "$poem" |grep -v 'she'
echo "$poem" |awk '!/she/'
echo "$poem" |sed -n '/she/!p'
echo "$poem" |sed '/she/d'
-------output----------
#1.Define a variable holding a multiline String
#2.OR condition example in regex
# use egrep, awk, sed respectively
Lily-like, white as snow,
she was a woman, so
Lily-like, white as snow,
she was a woman, so
Lily-like, white as snow,
she was a woman, so
Lily-like, white as snow,
she was a woman, so
#3.AND condition example in regex
# use egrep, awk, sed respectively
she was a woman, so
she was a woman, so
she was a woman, so
she was a woman, so
#4.NOT condition example in regex
# use egrep, awk, sed respectively
Lily-like, white as snow,
Lily-like, white as snow,
Lily-like, white as snow,
Lily-like, white as snow,
|
#1.Define a variable holding a multiline String
my $poem = <<"EOD";
Lily-like, white as snow,
she hardly knew
she was a woman, so
sweetly she grew.
EOD
my @poem = split(/\n/,$poem);
#2.OR condition example in regex
@result = grep { $_ =~ /^Lily|woman/ } @poem;
print "$_\n" foreach (@result);
#3.AND condition example in regex
@result = grep { $_ =~ /^she.*woman/ } @poem;
print "$_\n" foreach (@result);
#4.NOT condition example in regex
@result = grep { !/she/ } @poem;
print "$_\n" foreach (@result);
@result = grep ( !/she/ , @poem );
print "$_\n" foreach (@result);
foreach (@poem) { print "$_\n" unless /she/;}
-------output----------
#1.Define a variable holding a multiline String
#2.OR condition example in regex
Lily-like, white as snow,
she was a woman, so
#3.AND condition example in regex
she was a woman, so
#4.NOT condition example in regex
Lily-like, white as snow,
Lily-like, white as snow,
Lily-like, white as snow,
|
import re
#1.Define a variable holding a multiline String
poem='''Lily-like, white as snow,
she hardly knew
she was a woman, so
sweetly she grew.'''
#2.OR condition example in regex
print re.findall(r'^Lily.*|.*woman.*', poem, flags=re.M)
#3.AND condition example in regex
print re.findall(r'^she.*woman.*', poem, flags=re.M)
#4.NOT condition example in regex
print [x for x in poem.split('\n') if not 'she' in x]
-------output----------
#1.Define a variable holding a multiline String
#2.OR condition example in regex
['Lily-like, white as snow,', 'she was a woman, so']
#3.AND condition example in regex
['she was a woman, so']
#4.NOT condition example in regex
['Lily-like, white as snow,']
|