PROGRAMMING

  minte9
learningjourney




S R Q

Start, End

p08 Get into the habit of interpreting regex in a rather literal way.
 
#!/bin/sh

: "Get into the habit of interpreting regex
in a rather literal way

Do not think ^cat matches if you have:
a line with cat at the beginning

But rather ^cat matches if you have: 
the beginning of a line, followed by c, by a, ...

Grep understands bytes and lines
when no metacharacters, plain text search
"

A='Cat at the beggining'
B='At the end dog'
C='Birds everywhere bird'

echo $A | grep '^cat' -i -o | tee result.txt
echo $B | grep 'dog$' -i -o | tee result.txt -a
echo $C | grep 'bird' -i -o | tee result.txt -a
 
Cat
dog
Bird
bird
Javascript

Javascript

 
/**
 * Metacharacters ^$
 * 
 * A regular expression is written in the form of 
 *      /pattern/modifiers
 * 
 * The syntax is borrowed from Perl
 *      /g means all matches
 */

s1 = 'Cat at the beggining';
s2 = 'At the end dog';
s3 = 'Birds everywhere bird';

p1 = /^cat/ig;
p2 = /dog$/g;
p3 = /bird/gi;

m1 = s1.match(p1);
m2 = s2.match(p2);
m3 = s3.match(p3);

console.log(m1); // Cat
console.log(m2); // dog
console.log(m3); // Bird, bird

Questions    
Last update: 60 days ago
Grep, Classes