Elements of a Regular Expression¶

  • a sequence of characters: (no special syntax)
  • a specific character: the character itself
  • a set of characters: [], ., [^], [-], ...
  • repetition: {}, *, ?, +
    • == {0,}
    • == {1,} ? == {0,1}
  • anchor to start/end: ^, $
  • grouping: ()
  • alternatives: |

Question 4¶

This question tests your ability to write a regular expression.

Set the variable pat to a regular expression that matches part numbers created according to the following sequence:

begins with the letters AT
followed by an optional letter F
followed by a two- or three-digit number
followed by zero or more of the letters B, Q or Z, in any order
a dash (-)
a one- or two-digit number
In [1]:
import re
re.search( r'eifo|aaaa|kkkk', "abcdeifg zoo aaadvarkkkk") #.group(0)
Out[1]:
<re.Match object; span=(20, 24), match='kkkk'>
In [ ]:
 
In [2]:
pat=r'^ATF{0,1}\d{2,3}[BQZ]{0,}-[0-9]{1,2}'
import re
re.search(pat,'ATF22-2x')
Out[2]:
<re.Match object; span=(0, 7), match='ATF22-2'>

Question 4¶

Set the variable pat to a regular expression that matches part numbers created according to the following sequence:

a prefix of either JSM or CP
a single digit between 1 and 4
an optional letter S
the letter A
an optional letter H
a dash
the letters 12V or 24V

For example, re.search(pat,'JSM1SAH-12V') would return a match object but re.search(pat,'JSM1SAH-') would not.

In [3]:
import re
pat=r"(JSM|CP)[1-4]S{0,1}AH?-(12V|24V)"
re.search(pat,'JSM1SAH-')

Question 4¶

Set the variable pat to a regular expression that matches a part number string created according to the following sequence:

the letters CSTCE
a 1- or 2-digit number
the letter M
one or two zeros (0 or 00)
the letter G or V
a 2-digit number
an optional letter C
the characters -R0

For example, re.search(pat,'CSTCE50M00G00C-R0') would return a match object but re.search(pat,'CSTCE50M0G00C-R00') would not.

Question 4¶

A UK postcode consists of a sequence one to four alphanumeric characters, a space, one digit and two upper-case letters. For example, "EC1A 1BB". Set the variable pcre to a string containing a regular expression that matches UK postcodes.

In [4]:
pcre='[a-zA-Z0-9]{1,4} [0-9][A-Z]{2}'
import re
re.search(pcre,"EC1A 1BB")
Out[4]:
<re.Match object; span=(0, 8), match='EC1A 1BB'>

Question 4¶

An IP address consists of four numbers of between one and three digits separated by periods. For example, "10.0.0.1" or "192.168.1.1". Set the variable ipre to a regular expression that matches an IP address. Hint: note that a period in a regular expression matches any character.

In [5]:
import re
s="The s ab."
r=r'\sab'
m=re.search( r, s )
!dir 
 Volume in drive C is Local Disk
 Volume Serial Number is 9AE0-64CC

 Directory of C:\Users\Ed\SharedFolder\bcit\4653\notebooks\lectures

2022-05-18  02:10 PM    <DIR>          .
2022-05-18  02:10 PM    <DIR>          ..
2017-02-14  08:46 AM             1,045 .gitignore
2022-05-18  08:39 AM    <DIR>          .ipynb_checkpoints
2022-05-18  02:09 PM           604,439 00-Introduction.html
2017-02-14  08:46 AM            11,388 00-Introduction.ipynb
2022-05-18  02:09 PM           600,495 01-How-to-Run-Python-Code.html
2022-04-25  11:12 AM             6,952 01-How-to-Run-Python-Code.ipynb
2022-05-18  02:09 PM           629,922 02-Basic-Python-Syntax.html
2022-04-25  12:09 PM            16,130 02-Basic-Python-Syntax.ipynb
2022-05-18  02:09 PM           621,223 03-Semantics-Variables.html
2022-04-26  03:01 PM            13,067 03-Semantics-Variables.ipynb
2022-05-18  02:09 PM           648,937 04-Semantics-Operators.html
2022-04-27  10:56 AM            23,061 04-Semantics-Operators.ipynb
2022-05-18  02:09 PM           657,020 05-Built-in-Scalar-Types.html
2022-05-18  01:50 PM            17,299 05-Built-in-Scalar-Types.ipynb
2022-05-18  02:09 PM           655,379 06-Built-in-Data-Structures.html
2022-05-18  02:08 PM            22,545 06-Built-in-Data-Structures.ipynb
2022-05-18  02:09 PM           618,752 07-Control-Flow-Statements.html
2022-04-27  11:19 AM            11,130 07-Control-Flow-Statements.ipynb
2022-05-18  02:09 PM           636,526 08-Defining-Functions.html
2022-05-02  12:21 PM            14,112 08-Defining-Functions.ipynb
2022-05-18  02:09 PM           647,997 09-Errors-and-Exceptions.html
2022-05-02  12:21 PM            23,486 09-Errors-and-Exceptions.ipynb
2022-05-18  02:09 PM           655,291 10-Iterators.html
2022-05-04  11:19 AM            22,309 10-Iterators.ipynb
2022-05-18  02:09 PM           622,626 11-List-Comprehensions.html
2022-05-09  01:24 PM            11,736 11-List-Comprehensions.ipynb
2022-05-18  02:09 PM           637,538 12-Generators.html
2022-05-09  01:24 PM            15,019 12-Generators.ipynb
2022-05-18  02:09 PM           614,509 13-Modules-and-Packages.html
2022-05-06  09:47 AM            11,031 13-Modules-and-Packages.ipynb
2022-05-18  02:09 PM           731,935 14-Strings-and-Regular-Expressions.html
2022-05-16  04:14 PM            54,478 14-Strings-and-Regular-Expressions.ipynb
2022-05-18  02:09 PM           649,221 15-Preview-of-Data-Science-Tools.html
2017-02-14  08:46 AM           181,717 15-Preview-of-Data-Science-Tools.ipynb
2022-05-18  02:09 PM           595,587 16-Further-Resources.html
2017-02-14  08:46 AM             5,257 16-Further-Resources.ipynb
2022-05-18  02:10 PM           613,503 17-Figures.html
2017-02-14  08:46 AM            16,736 17-Figures.ipynb
2022-05-11  10:01 AM    <DIR>          2022
2022-05-18  02:10 PM           612,735 Classes.html
2022-05-18  11:51 AM             8,876 Classes.ipynb
2022-05-16  09:20 AM                31 data.txt
2022-05-18  02:10 PM           595,085 DynamicTyping.html
2022-05-16  09:25 AM             2,338 DynamicTyping.ipynb
2017-02-14  08:46 AM    <DIR>          fig
2022-05-18  02:10 PM           595,946 FileIO.html
2022-05-16  04:14 PM             2,655 FileIO.ipynb
2022-05-18  02:10 PM           593,584 Index.html
2017-02-14  08:46 AM             3,708 Index.ipynb
2022-04-25  10:18 AM    <DIR>          lectures.2021
2017-02-14  08:46 AM             6,555 LICENSE
2022-05-11  10:01 AM    <DIR>          New folder
2022-05-18  02:10 PM               160 outdata.txt
2017-02-14  08:46 AM             4,648 README.md
2022-05-18  02:10 PM           596,275 Recursion.html
2022-05-18  11:51 AM             3,445 Recursion.ipynb
2022-05-11  09:15 AM             8,693 RegularExpressions.ipynb
2022-05-06  10:20 AM            11,617 Scope.ipynb
2022-05-18  11:51 AM             2,887 Scripts.ipynb
2022-05-18  11:51 AM             3,267 Sorting.ipynb
2017-02-14  08:46 AM    <DIR>          tools
2022-05-18  11:51 AM             6,194 Unicode.ipynb
2022-04-26  03:44 PM             1,670 Untitled.ipynb
              57 File(s)     14,979,767 bytes
               8 Dir(s)  50,280,349,696 bytes free