Documente Academic
Documente Profesional
Documente Cultură
Example1: List all files that start with the name ‘file’. g. file, file1, file2,
filenew
$ ls file*
Example2: List all files that end with the name ‘file’. g. file, afile, bfile,
newfile
$ ls *file
Example1: List all files that have one character after ‘file’. g. file1,
file2, filea
$ ls file?
Example2: List all files that have two characters before ‘file’. g. dofile,
tofile, a1file
$ ls ??file
Example1: List all files that have a single digit after ‘file’. g. file1, file2
$ ls file[0-9]
Example2: List all files that have anyone letter before ‘file’. g. afile,
zfile
$ ls [a-z]file
Example1: List all files that have digits after ‘file’. g. file1, file2, file33
$ ls file[0-9]*
The expressions use special characters to match the expression with one
or more lines of text.
The pattern is constructed using a series of characters and special
characters representing anchors, character-sets, and modifiers.
Regular expressions can be used with text processing commands like vi,
grep, sed, awk, and others.Note that although some regular-expression
patterns look similar to filename-matching patterns – the two are unrelated.
#1) ‘^’ – anchor character for start of line:
If the carat is the first character in an expression, it anchors the remainder
of the expression to the start of the line.
This pattern matches the set of characters specified between the square
brackets.
Example1: Match all lines that contain ‘ha’ followed by zero or more
instances of ‘p’ and then followed by ‘y’. g. “happpy” or “hay”
Pattern: ‘hap*y’
Example2: Match all lines that start with a digit following zero or more
spaces E.g. “ ” or “2.”
$ grep “ *[0-9]” file1
#7) ‘?’ – ‘zero or one’ modifier:
This modifier matches with zero or one instances of the preceding
character-set.
Example1: Match all lines that contain ‘hap’ followed by zero or one
instances of ‘p’ and then followed by ‘y’. g. “hapy” or “happy”
Pattern: ‘happ?y’
Example2: Match all lines that start with a digit followed by zero or
one ‘:’ characters E.g. “1” or “2:”
$ grep “^[0-9]:?” file1
Metacharacters
Metacharacters consist of non-alphanumeric symbols such as:
. \\\ | ( ) [ { $ * + ?
# substitute $ with !
sub(pattern = "\\$", "\\!", "I love R$")
## [1] "I love R!"
Sequences
To match a sequence of characters we can apply short-hand notation which
captures the fundamental types of sequences. The following displays the
general syntax for these common sequences:
# substitute any digit with an underscore
gsub(pattern = "\\d", "_", "I'm working in RStudio
v.0.99.484")
## [1] "I'm working in RStudio v._.__.___"