Changes

Jump to: navigation, search

Tutorial9: Regular Expressions

117 bytes added, 12:57, 2 September 2020
KEY CONCEPTS
===Regular Expressions vs. Filename Expansion===
<table align="right"><tr valign="top"><td>[[Image:re-1.png|thumb|right|350px|'''Filename expansion symbols ''' allow the Linux shell to expand filenames as arguments (referred to as “globbing”).]]</td><td>[[Image:re-2.png|thumb|right|250px|Output of the '''ls -lRConcept''' command to display directory structureof matching a simple pattern of text contained within a text file.]]</td></table>In a previous lesson, you learned that '''filename expansion symbols ''' allow the Linux shell to expand filenames as arguments (referred to as “globbing”) for file management commands. This is very useful for managing multiple files sharing similar characteristics such as the same file extension.
''Filename Expansion symbols are symbols'' are used to search, edit and manipulate text and are used with Linux file management commands such as '''ls''', '''rm''', '''mv''', '''cp''', '''cat''', '''less''' and '''more'''. This can represent text contained in files or text as a result of issuing Linux commands using a pipeline.
A '''regular expression''' is a combination of two types of characters: '''literals''' and '''special characters'''.<br>In combination, these characters define a logical pattern. Strings of text can be compared to this pattern<br>to see if they fit the pattern defined by the expression.
[[Image:re-3.png|thumb|right|250px|The simplest regular expression is a series of letters and numbers, possibly including white space (tabs or space characters).]]The simplest regular expression is a series of letters and numbers, possibly including white space (tabs or space characters), that have no special meaning. Such a regular expression consists of ``literals''; that is, normal letters, which match only an identical letter in the data being searched.<br><br>For example:
"This is a regular expression"<br><br>
When an editor searches for a '''literal regular expression''', it can only score a ``hit'' if it finds exactly that sequence of characters in the data it is searching. <br>Reference: http://osr507doc.xinuos.com/en/OSUserG/_Literal_chars_in_regexps.html
===Complex Regular Expressions===
The problem with using literals or simple regular expressions is that <u>general </u> patterns are matched.
For example, the pattern “'''the'''” would be matched for larger word containing that pattern like '''there''', '''they''', '''either''', '''them''', '''their''', etc. <br>Another problem is that you may want to search for pattern at a specific location within the string of text (like at the beginning or end).
There are other regular expression tools to provide more precise matches. These tools are '''complex''' and '''extended''' regular expressions.
'''Complex Regular Expressions ''' use symbols to help match text for more <u>precise or </u> (complex ) patterns.<br>The most common complex regular expression symbols are displayed below:
:*'''Anchors''' '''^''' , '''$'''
Examples of how to use these complex regular expressions with the grep command are displayed below:
<table align="left"><tr valign="top"><td>[[Image:re-4.png|thumb|right|220px|Example of using '''anchors'''.]]</td><td>[[Image:re-5.png|thumb|right|200px|Example of matching charactersby '''character(s)'''.]]</td><td>[[Image:re-6.png|thumb|right|220px|Example of using '''character class'''.]]</td><td>[[Image:re-7.png|thumb|right|200px|Example of matching '''zero or more occurrenceof preceding character'''.]]</td></tr></table>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
===Extended Regular Expressions===
'''Extended Regular Expressions ''' consist of additional special characters the “extend” the capability of regular expressions.
'''Repetition {min,max}''' , '''?''' , '''+'''<br>
Allows for more precise repetitions. Using braces, you can specify the minimum and maximum number of repetitions.
Examples of how to use these complex regular expressions with the grep command are displayed below:
<table align="left"><tr valign="top"><td>[[Image:re-8.png|thumb|right|280px|Example of using '''repetition'''.]]</td><td>[[Image:re-9.png|thumb|right|250px|Example of using '''grouping'''.]]</td><td>[[Image:re-10.png|thumb|right|250px|Example of using '''or condition with grouping'''.]]</td></tr></table>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
13,420
edits

Navigation menu