Python Lesson 18: Intro to NumPy (NumPy pt. 1)

Hello everyone,

Michael here, and today’s Python lesson will be a little different than the Python lessons we’ve done before. Now that I’ve covered some of the basic building blocks of Python, I figured it’s time to dive into some cooler stuff (e.g. data analytics, natural language processing, etc.) that you can do with Python. Right now, I’ll start with diving into the data analytics side of Python.

Now, let’s get started with learning the basics of one of Python’s most basic data analytics packages-NumPy. NumPy is a special Python package that is used for working with numerical arrays-NumPy is actually short for “numerical Python”.

However, to use NumPy, you need to be sure that it’s installed on your computer. To install a Python package to your computer, you would need to pip install the package on your computer’s command prompt like so:

If you’re wondering what pip is, it’s the package that Python uses to install other packages (think of pip as Python’s main package manager). Oftentimes, pip will already come installed with whatever Python IDE you chose to use (I use both Jupyter Notebook and Anaconda in my development), but in case it isn’t, here’s the link to download pip onto your computer-https://pypi.org/project/pip/.

To install the NumPy package, simply type this line onto your command prompt-pip install numpy. Sometimes, there are certain packages already pip installed on your laptop, so you’ll get a message on the command prompt that says Requirement already satisfied:, as I did above (apparently NumPy was already pip installed on my computer).

  • If you wanted to uninstall NumPy (or any other Python package), simply type pip uninstall numpy (you can replace this with any other python package you pip installed) onto the command prompt. The pip package manager will then ask you if you want to uninstall the python package.
  • You don’t have to pip install Python packages in any particular directory, but I’d recommend that you perform any pip installs in your main directory (the directory with the structure “C:/Users/[your username]”)

Now, if you wanted to see a list of all the Python packages that are installed on your computer (along with the corresponding versions of each package), type pip list onto the command prompt like so:

As you can see, I have numpy version 1.18.5 installed on my computer.

  • Depending on the Python library you need for your project, running pip list can help save you a pip install.

Now that I’ve covered all the pip install material, let’s dive right in to NumPy!

To use NumPy in your IDE, first run this line of code-import numpy as np. The as np part is completely optional since np is simply the alias for numpy. Including the as np part in your import statement allows you to refer to the NumPy package as np rather than numpy (in other words, as np is just convenient shorthand for NumPy).

Now here’s an example on how to create a simple NumPy array:

import numpy as np

array1 = np.array([2,4,6,8,10])

print(array1)

[ 2  4  6  8 10]

To create a NumPy array, simply use the np.array() function and use an array-like object as the parameter for this function. In this example, I used the list [2, 4, 6, 8, 10] as the parameter for the np.array() function.

  • For your information, NumPy arrays are stored with the type ndarray.

Now, I did mention that you can use an array-like object as the parameter for the np.array() function. Lists aren’t the only parameter accepted by this function; tuples work great too. Here’s an example of using a tuple in the np.array() function:

array2 = np.array((1,2,3))

print(array2)
print(type(array2))

[1 2 3]
<class 'numpy.ndarray'>

In this example, the tuple I passed into the np.array() function is converted into a NumPy array (of type ndarray). The magic of the np.array() function is that it can convert any array-like object (like a list or a tuple) into a NumPy array.

Pretty cool stuff right? Now, did you know that you can create multi-dimensional arrays, which are arrays with extra levels of array depth. Put simply, multi-dimensional arrays can contain any number of nested arrays, which are arrays within arrays.

Here’s how to make a 0-D (0-dimensional) array:

array3 = np.array(50)

print(array3)

50

Looks simple right? 0-dimensional arrays consist of a single element. That’s it.

Now, if you want to know to create a 1-D array, refer to the examples with array1 and array2, as these are two excellent examples of 1-D NumPy arrays. 1-D arrays consist of array-like objects (like lists or tuples) as their elements.

Now here’s an example of how to create a 2-D array:

array4 = np.array([[1, 3, 5], [7, 9, 11], [13, 15, 17]])

print(array4)
print(array4.ndim)

[[ 1  3  5]
 [ 7  9 11]
 [13 15 17]]
2

In this example, I created a 2-D array by wrapping a list of three 1-D arrays inside a pair of square brackets ([]). As for the ndim call, it simply shows you how many dimensions are in a NumPy array (2 in this case).

Now here’s an example of how to create a 3-D array:

array5 = np.array([[[3, 6, 9], [12, 15, 18]], [[3, 6, 9], [12, 15, 18]]])

print(array5)

[[[ 3  6  9]
  [12 15 18]]

 [[ 3  6  9]
  [12 15 18]]]

A NumPy 3-D array consists of several 2-D arrays; each 2-D array in the 3-D array is printed on a separate line, as you can see above.

  • Getting the brackets right for 3-D arrays does get tricky, so keep that in mind.

Now I’ve shown you how to create 0-D, 1-D, 2-D, and 3-D arrays. However, NumPy arrays aren’t limited to a 3-dimension maximum; the sky’s the limit when it comes to the amount of dimensions a NumPy array can have.

Let’s say we wanted to create a 6-dimensional NumPy array. How would we go about doing that? Take a look at the code below:

array6 = np.array([-10, -8, -6, -4, -2, 0], ndmin=6)

print(array6)

[[[[[[-10  -8  -6  -4  -2   0]]]]]]

To add X number of dimensions to a NumPy array, simply create a 1-D array and specify the number of dimensions you want in the array using the ndmin parameter.

As you can see, since I created a 6-dimensional array in the above example, there are six pairs of square brackets surrounding the array. Just for fun, let’s see what happens when we create a 50-dimensional array (using the same elements as array6):

array7 = np.array([-10, -8, -6, -4, -2, 0], ndmin=50)

print(array7)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-f4b851766b21> in <module>
----> 1 array7 = np.array([-10, -8, -6, -4, -2, 0], ndmin=50)
      2 
      3 print(array7)

ValueError: ndmin bigger than allowable number of dimensions NPY_MAXDIMS (=32)

As you can see, that didn’t work. Apparently NumPy arrays can only have 32 dimensions tops. Even an experienced Python coder like me learns something new everyday.

Thanks for reading,

Michael

Python Lesson 17: Date & Time Manipulation in Python

Hello everybody,

Michael here, and today’s lesson will be on performing date & time manipulation in Python. Now I know I did a date & time manipulation lesson in R (check out R Lesson 19: Fun With Dates & Times), but I want to show you how it’s done in Python.

Before I get started with any coding examples, I think it will be important to mention that dates aren’t data types. However, we can import a datetime module to create date & time objects; to import the datetime module, use the line import datetime.

Now, here’s how to use the datetime module to display the current date and time:


now = datetime.datetime.now()

print(now)

2021-04-15 22:16:45.356841

In this example, I created a datetime object-now-that displays the current date and time (using the 24-hour format for time) using the datetime.now() method of the datetime module. Simple enough, right?

Well, along with displaying the current date & time, the datetime module also has several format codes to retrieve information about a datetime object. Let’s say we wanted to retrieve the current month (fully spelled out) and the current day of the week (in shorthand). Here’s how to do so:

now = datetime.datetime.now()

print(now.strftime("%a"))
print(now.strftime("%B"))

Thu
April

In this example, I am retrieving the current day of the week (in shorthand) and the current month (fully spelled out) from the current datetime-April 15, 2021 10:16:45PM. To retrieve the shorthand day of the week, I used the format code %a and to retrieve the fully spelled out month, I used the format code %B. Also, in order to use these format codes, I need to use them as parameters in the function strftime.

Here’s a list of all the format codes you can use with the strftime function (all examples refer to the now object):

  • %a-returns the shorthand version of the weekday.
    • example: %a would return Thu (which is shorthand for Thursday)
  • %A-returns the fully spelled out version of the weekday
    • example: %A would return Thursday (the name of the weekday spelled out)
  • %w-returns the corresponding number of the weekday; the weekdays are numbered from 0-6, with 0 being Sunday and 6 being Saturday
    • example: %w would return 4 (Thursday corresponds to 4)
  • %d-returns the day of the month (which can range from 01-31)
    • example: %d would return 15 (the day of the month in the now object is 15)
  • %b-returns the shorthand version of the month
    • example: %b would return Apr since the month for the now object is April.
  • %B-returns the fully spelled out version of the month
    • example: %B would return April (which is the month for the now object)
  • %m-returns the number of the month (which can range from 01 to 12):
    • example: %m would return 04 since April is the 4th month of the year
  • %y-returns the last two digits of the year:
    • example: %y would return 21 since the year is 2021
  • %Y-returns the full version of the year:
    • example: %Y would return 2021 since the year is 2021
  • %H-returns the hour according to a 24-hour clock:
    • example: %H would return 22 since the time is 10:16 PM.
  • %I-returns the hour according to a 12-hour clock:
    • example: %I would return 10 since the time is 10:16 PM.
  • %p-returns AM or PM depending on whether the time is before or after 12 noon.
    • example: %p would return PM since the time is 10:16 PM.
  • %M-returns the minute of the time part of the datetime object (ranging from 00-59)
    • example: %M would return 16 since the minute of the time part of now is 16.
  • %S-returns the second of the time part of the datetime object (ranging from 00-59)
    • example: %S would return 45 since the second of the time part of now is 45 (the full time is 10:16:45 PM)
  • %f-returns the millisecond part of the datetime object (ranging from 000000-999999)
    • example: %f would return 356841 since the millisecond part of now is 356841 (the full time with milliseconds is 10:16:45.356841 PM)
  • %z-returns the difference between the current time and UTC time (in hours)
    • example: I know I didn’t set a time-zone for now, but if I did, %z would return -0600 since I’m writing this post from the US Central Time Zone and US CST is 6 hours behind UTC.
  • %Z-returns the time-zone
    • example: %Z would return CST as I’m currently in the US Central Time Zone (this would work if I had set a time zone)
  • %j-returns the day number of the year (ranging from 001-366)
    • example: %j would return 105 as April 15 is the 105th day of the year.
    • An important thing to note here is that if the date for now was set to April 15, 2020, %j would return 106 since April 15 is the 106th day of the year in leap years.
  • %U-returns the week number of the year (ranging from 00-53); this format code assumes weeks start on Sunday
    • example: %U would return 20 as April 15, 2021 falls on the 20th week of the year 2021.
    • The %U format code would return 01 for January 3-9, 2021, as this is the first full week of the year. However, for January 1-2, 2021, %U returns 00. Interestingly, for December 27-31, 2020 (which falls on the same week as January 1-2, 2021), %U would return 52.
  • %W-returns the week number of the year (ranging from 00-53); unlike the format code %U, this format code assumes weeks start on Monday
    • example: %W would also return 20 since it is still the 20th week of the year 2021 (even though %W uses the weeks-starting-on-Monday system)
  • %c-returns the local date and time; the local date and time is displayed using this format-(day of week) (month & day) (local time in 24-hour format) (year)
    • example: %c would return Thu Apr 15 22:16:45 2021 as that is the local time stored in the now object.
    • Only the hours, minutes, and seconds are returned from the local time.
  • %x-returns the date in mm/dd/yy format
    • example: %x would return 04/15/21
  • %X-returns the local time in 24-hour format
    • example: %X would return 22:16:45
  • Just as with regex special sequences, the letter casing can be very easy to mix up between format codes, so keep that in mind.
  • Date format codes start with percent signs while regex special sequences start with backslashes (it can be very easy to confuse date format codes with regex special sequences).

Now, what if I wanted to create my own datetime object? Here’s how to do so:

date1 = datetime.datetime(2021, 4, 18)

print(date1)

2021-04-18 00:00:00

In this example, I created my datetime object date1 and set the parameters equal to 2021, 4, and 18, respectively. When I printed the date1 object, I got the output 2021-04-18 00:00:00.

How did I get this output? Well, since I used 2021, 4, and 18 as parameters, I got the date 2021-04-18 since the year is the first parameter to set when creating a datetime object followed by the month and day.

However, you’re probably wondering how I got the time 00:00:00. See, when I create a datetime object, I can not only set a date but I can also set a time. Aside from the year, month, and day, I can also set an hour, minute, second, millisecond, and time zone parameter. All of these parameters are optional when creating a datetime object, and if no value is specified for each of these parameters, 00 is the default value-hence why the outputted time was 00:00:00.

Now that I’ve shown you the basic things you can do with the datetime package, let’s discuss how to perform date manipulation.

First of all, what if we wanted to calculate the difference between two dates? Here’s how we would do so:

date2 = datetime.datetime(2021, 9, 9)
date3 = datetime.datetime.now()
delta = date2 - date3

print("There are" ,delta.days, "more days until the 2021 NFL season.")
There are 143 more days until the 2021 NFL season.

In this example, I created two datetime objects-date2 and date3-and created a variable delta to determine the difference between the two dates.

  • Just so you know, the date being referenced in now is April 18, 2021. Also, the 2021 NFL season starts on September 9, 2021, hence why the date2 parameters are (2021, 9, 9).

After subtracting the two datetime objects, I then get the message There are 143 more days until the 2021 NFL season. Just for reference, here is the print statement I used: print("There are" ,delta.days, "more days until the 2021 NFL season."). The delta.days statement returns the number of days between the two datetime objects.

Now, what if you wanted to find the amount of weeks between two dates? Here’s how to do so:

date2 = datetime.datetime(2021, 9, 9)
date3 = datetime.datetime(2021, 4, 18)
delta = date2 - date3

print("There are" ,round(delta.days/7, 1), "more weeks until the 2021 NFL season.")

There are 20.6 more weeks until the 2021 NFL season.

Unfortunately, finding the number of weeks between two dates isn’t as simple as using delta.weeks (I know because I tried this). If you want to find the amount of weeks between two dates, simply divide the result of delta.days by 7. The decimal generated by delta.days/7 is quite long, so it’s often useful to round the decimal (using the round function) to either 1 or 2 decimal places as I did above.

Now let’s see how we can get the amount of months between two dates:

date2 = datetime.datetime(2021, 9, 9)
date3 = datetime.datetime(2021, 4, 18)
delta = date2 - date3

print("There are" ,round(delta.days/30, 1), "more months until the 2021 NFL season.")

There are 4.8 more months until the 2021 NFL season.

In this example, I used the same approach to finding the amount of months between two dates as I did when I was trying to find the amount of weeks between two dates (even rounding the quotient to one decimal place). The only difference between this example and the previous example is that I divided delta.days by 30 to find the amount of months between the two dates.

  • You probably guessed this, but there is no delta.months function. Hence why we need to divide the result of delta.days by 30.
  • In order to get the amount of months between two dates, you don’t have to divide delta.days by 30. 28 might work, but when I tried dividing delta.days by 28 in the above example, I got 5.1 (I felt that 4.8 was more accurate).

Now I’ve shown you how to create datetime objects as well as calculate the difference (in days, weeks, and months) between two datetime objects. What if we wanted to find out the date that would be X days before or after a certain date? Here’s how to do so:

from datetime import timedelta

date4 = datetime.datetime.now()

newdate = date4 + timedelta(days=20)

print(newdate)

2021-05-09 22:10:53.372408

In order to add or subtract days from a certain date, I would first need to import the timedelta module from the datetime package (use the line from datetime import timedelta). In this example, I created a datetime object date4 and set it to the current date & time (April 19, 2021 at 10:10 PM). I then created a newdate object and set it equal to the value of date4 plus 20 days from date4 using the timedelta(days=20) function-I then got the result of 2021-05-09 22:10:53.372408.

Now, what if you wanted to subtract 20 days from date4? Here’s how you would do so using the timedelta function:

date4 = datetime.datetime.now()

newdate = date4 - timedelta(days=20)

print(newdate)

2021-03-30 22:20:01.464021

In order to find out the datetime that falls 20 days before date4, all I needed to do was replace the plus sign with a minus sign and VOILA!-I get the new datetime (March 30, 2021 at 10:20 PM).

  • Using this line of code for newdatenewdate = date4 + timedelta(days=-20)-would’ve worked as well.
  • Unfortunately, you can only add or subtract dates with timedelta-multiplying, dividing, or raising dates to a certain power won’t work with timedelta (I know because I tried this and kept getting TypeErrors).

The timedelta function not only calculates days before or after a certain date. You can also calculate weeks, hours, minutes, seconds, milliseconds, and nanoseconds (yes, timedelta is that precise) before or after a certain datetime.

  • There are 1 million nanoseconds in a millisecond, in case you’re wondering.

Thanks for reading,

Michael

Python Lesson 16: Regular Expressions in Python

Hello everybody,

It’s Michael, and today’s post will cover regular expressions in Python. I know I already did a Java lesson on RegEx (the colloquial term for regular expressions-here’s the link to that lesson: Java Lesson 18: RegEx (or Regular Expressions)) but I wanted to cover how to use regular expressions in Python, so here goes.

To start working with regular expressions in Python, import the regular expressions module using this line of code-import re.

Now, here’s a simple example of regular expressions in Python:

text = "Jupyter Notebook is awesome!!!"

print(re.findall('e', text))

['e', 'e', 'e', 'e']

In this example, I have a String that reads Jupyter Notebook is awesome!!!. In the print expression, I’m using the re module’s findall function to find all of the e’s in the text String. The print expression then returns a list of all of the e’s found in text, of which there were four.

  • To use any of the re module’s functions, you’ll need two parameters-the string/character/pattern you want to search for and the String where you want to search for that particular string/character/pattern.

Now, let’s try a more complex RegEx (the colloquial name for regular expressions) example in Python:

text2 = "Tonight is a very beautiful spring night"

print(re.search("ht$",text2))

<re.Match object; span=(38, 40), match='ht'>

In this example, I am using the search function to find the pattern ht$ anywhere in the string. An important thing to note is that, unlike the findall function, the search function only looks for one pattern/string/character match in the string being analyzed (in this case, text2).

  • Similar to the findall function, you’ll need to include two parameters for the search function-the pattern/string/character you’re searching for and the string where you are looking for the pattern/string/character.

Now, you’re probably wondering what ht$ means. The dollar sign ($) is called a metacharacter and in the context of regular expressions, metacharacters help define more specific search criteria for a pattern/string/character you are looking for. In the case of ht$, the search function is looking for any part of the text2 string that ends with ht; a Match object is returned if the search function finds any part of the String that ends with ht. In this case, a Match object was returned; the span attribute of the Match object will tell you where in the string a match was found (span shows you index positions in the String where the match was found). For text2, the match starts at index 38 and ends at index 39 (index 40 won’t be considered part of the match)-in other words, the match is found between the 39th and 40th positions in text2 (recall that string indexing starts with 0).

Here’s a list of other useful regex metacharacters:

  • []-find a set of characters
    • example: [a, e, i, o, u, y] can be used to find all of the vowels in text2 (and yes I’ll consider y as a vowel)
  • \-use a special sequence (more on this later)
  • .-find any character (except the \n newline character)
    • example: n...t can be used in text2 to find any word in the string that starts with n, ends with t, and has any three letters in between
  • ^-find any part of the string that starts with a certain character/pattern
    • example: ^To can be used to find any part of text2 that starts with “To”
  • $-find any part of the string that ends with a certain character/pattern (I explained this metacharacter in the above example)
  • *-find none (or more) occurrences of a certain character/pattern in a string
    • example: ni* can be used to find any amount (or no amount) of occurrences of the pattern “ni” in text2.
  • +-find at least one occurrence of a certain character/pattern in a string
    • example: ni+ can be used to find at least one occurrence of the pattern “ni” in text2
  • {}-find a specific number of occurrences of a certain character/pattern in a string
    • example: ni{1} can be used to find one AND ONLY ONE occurrence of the pattern “ni” in the string text2.
  • |-find a match that contains either pattern/string
    • example: tonight|today can be used to find out whether text2 contains either tonight or today.
    • note: this is the same operator that you’d use as an OR statement in conditional logic but it takes a slightly different albeit conceptually similar meaning when dealing with Python regex.

Now I know I mentioned special sequences in the list above, so let’s see some special sequences in action:

text3 = "Today's date is 04/10/2021"

print(re.split("\d{2}/\d{2}/\d{4}", text3))

["Today's date is ", '']

In this example, I’m using the split function to search for any part of the string text3 with the pattern \d{2}/\d{2}/\d{4}. You’re probably wondering what the split function does or what the pattern \d{2}/\d{2}/\d{4} means.

First of all, the split function, like the findall and search functions, takes in a pattern to search for along with the string where the function will look for the specified pattern. However, the split function is different because it doesn’t search for matches; rather, split returns a list of elements between each string split.

The expression \d{2}/\d{2}/\d{4} uses a group of special sequences. In the context of regular expressions, special sequences are represented with backslashes followed by an individual character that serve as convenient shorthand for pre-defined character classes. For instance, the special sequence \d looks for digits in the string; when combined with the metacharacter {2}, \d{2} looks for any sequence of two digits in a string. In the expression \d{2}/\d{2}/\d{4} , the split function is looking for a pattern that starts with two digits followed by a forward slash followed by another digit pair followed by another forward slash and ending with a sequence of four digits.

Here is a list of all the special sequences that Python regex uses:

  • \A-looks for a match if certain character(s) are at the beginning of the string
    • example: \ATo can be used to see if text3 starts with the characters “To”
  • \b-looks for a match if certain character(s) are at the beginning or end of a word
    • example: \bda can be used to see if there are any words in text3 that start with the characters “da”. However, if you want to see if “da” can be found at the end of a word, use the syntax da\b
  • \B-looks for a match if certain character(s) are present BUT NOT at the beginning or end of a word
    • example: \Bda can be used to see if there are any words in text3 that contain but don’t begin with the characters “da”. Likewise, da\B can be used to see if there are any words in text3 that contain but don’t end with the characters “da”.
  • \d-looks for digits in the string (I discussed this sequence in the example above)
  • \D-looks for non-digits in the string
    • example: \D can used to return all of the non-digit characters in text3.
    • Yes, whitespace counts as a character too.
  • \s-looks for all of the whitespace characters in the string
    • example: \s can be used to return a list of all the whitespace characters in text3, of which there are three.
  • \S-looks for all of the non-whitespace characters in the string
    • example: \S can be used to return a list of all the non-whitespace characters in text3
  • \w-looks for all of the word characters in the string; in case you’re wondering, the word characters are the letters of the alphabet, the digits 0-9, and the underscore (_)
    • example: \w can be used to return a list of all the word characters in text3
  • \W-looks for all of the non-word characters in the string (in other words, anything that’s not a letter, digit, or underscore)
    • example: \W can be used to return a list of all the non-word characters in text3
  • \Z-looks for a match if certain character(s) are at the end of the string
    • example: 21\Z can be used to see if text3 ends with the characters “21”.
  • Whenever you’re using special sequences, be sure not to mix up letter cases, as capital letters and lowercase letters will do different things in the context of regex special sequences!

Now I’ve shown you how to split a string with regex, find all instances of a certain character pattern in a string, and retrieve a match object using regex. However, what if you wanted to replace one character pattern with another? Here’s an example of this:

text4 = "Today was a beautiful Monday afternoon!"

print(re.sub("Mon", "Tues", text4))

Today was a beautiful Tuesday afternoon!

If you want to replace one character pattern with another, use the sub method. The difference between this method and the other three regex methods (findall, search, and split) I discussed earlier is that sub takes three parameters while the other methods only take two; the three parameters sub uses are the character pattern you want to replace, the new character pattern you want to use, and the string where you want to make the switch (in that order). In this example, I’m replacing the character pattern “Mon” with the pattern “Tues” in text4 to change the string from Today was a beautiful Monday afternoon! to Today was a beautiful Tuesday afternoon!

Now, before I go, I want to discuss one more Python regex concept-sets. In regex, sets are sets of characters inside square brackets with a special meaning.

Let’s check out an example of sets below:

text5 = "His address is 742 Evergreen Terrace. His phone number is 413-234-9080. His date of birth is 09/12/1971"

print(re.findall("[0-9][0-9]", text5))

['74', '41', '23', '90', '80', '09', '12', '19', '71']

In this example, I am using the set [0-9][0-9] to find all two-digit sequences in text5.

You’re probably wondering what [0-9][0-9] does in the context of regex. The set [0-9][0-9] looks for all two-digit sequences in text5 between 00 and 99.

One interesting things about sets is that, between them, special sequences, and metacharacters, they are the most customizable of the three regex elements (though you could argue that metacharacters are widely customizable as well). In the example above, I could’ve used the set [0-9][0-9][0-9] to look for all three-digit sequences in text5 between 000 and 999. But what if I didn’t want to use the 00-99 digit sequence range? Let’s say I wanted to look for all two-digit sequences in text5 between 00 and 49; all I need to do is specify the set [0-4][0-9] in the first parameter of the findall function.

What other regex sets can you use with Python? Here’s a list of them:

  • [ber]-looks for all the B’s, E’s, and R’s in the string
  • [b-r]-looks for all the lowercase letters between b and r in the string
    • If you wanted to modify this search to find capital letters, use the set [B-R], which finds all of the capital letters between B and R in the string.
  • [^one]-looks for all of the characters that aren’t o, n, or e.
  • [4567]-looks for all of the 4’s, 5’s, 6’s, and 7’s in the string
  • [0-9]-looks for any digit between 0 and 9 in the string
  • [0-6][0-9]-looks for any two-digit sequence between 00 and 69 in the string (I discussed this set concept in the above example)
  • [b-rB-R]-looks for every letter between b and r in the string, both lowercase and uppercase
  • [$]-looks for all dollar sign characters ($) in the string

Thanks for reading,

Michael