<jupyter_start><jupyter_text>QuantifiersThis notebook continues coverage of the special characters by focusing on those that are used to match repeated characters. These group of metacharacters are called **quantifiers**, because they quantify they type of repetition you desire. The asterisk metacharacter `*`The **asterisk** or **star** metacharacter matches the previous character 0 or more times. For instance, the regex, `'Ah* No'` will look for strings that have an uppercase 'A' followed by 0 or more lowercase 'h' followed by ' No'. Let's see how this works on a Series of sample data:<jupyter_code># Create Series of fake data
import pandas as pd
s = pd.Series(['Ouch', 'Ah No', 'Ahh', 'Nooo', 'Ahhhhhhh No', 'A No', 'A'])
s
pattern = 'Ah* No'
filt = s.str.contains(pattern)
s[filt]<jupyter_output><empty_output><jupyter_text>Without the ' No' at the end, it would match two more values:<jupyter_code>pattern = 'Ah*'
filt = s.str.contains(pattern)
s[filt]<jupyter_output><empty_output><jupyter_text>The plus metacharacter `+`The **plus** metacharacter is very similar to the asterisk, except that it matches 1 or more of the previous character. Thus for the regex `'Ah+ No'`, the 'h' must appear at least once.<jupyter_code>pattern = 'Ah+ No'
filt = s.str.contains(pattern)
s[filt]<jupyter_output><empty_output><jupyter_text>The question mark metacharacter `?`The question mark is similar to both the asterisk and the star, except that it matches the previous character 0 or 1 times exactly.<jupyter_code>pattern = 'Ah? No'
filt = s.str.contains(pattern)
s[filt]<jupyter_output><empty_output><jupyter_text>Using another example, the regex `'Sec?r'` will match both 'Secret' and 'Serving'. Basically, the character before the question mark is **optional**.<jupyter_code>movie = pd.read_csv('../data/movie.csv')
title = movie['title']
pattern = 'Sec?r'
filt = title.str.contains(pattern)
title[filt].head()<jupyter_output><empty_output><jupyter_text>The curly braces metacharacter `{m,n}`The curly braces metacharacter matches the previous character a given number of times. There are four different ways to use the curly braces:* a single integer `a{3}` - matches exactly three 'a' characters in a row* a single integer followed by a comma `a{3,}` - matches three or more 'a' characters in a row* a comma followed by a single integer `a{,3}` - matches zero to three 'a' characters in a row* two integers separated by a comma `a{3,5}` - matches between 3 and 5 'a' characters in a rowLet's create another Series by hand and match all the strings that begin with 'A', have the letter 'h' repeat between 2 and 5 times and then followed by ' No'.<jupyter_code>s = pd.Series(['Ouch', 'Ahhh No', 'Ahh No', 'Nooo', 'Ahhhhhhh No', 'A No', 'A', 'Ahhh'])
s
pattern = 'Ah{2,5} No'
filt = s.str.contains(pattern)
s[filt]<jupyter_output><empty_output><jupyter_text>ExercisesUse the title column of the movie Series for these exercises.<jupyter_code>movie = pd.read_csv('../data/movie.csv')
title = movie['title']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1145867632532104, 'fraction_numerical': 0.005268356931182087, 'mean_word_length': 5.796420581655481, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '542243', 'license': 'MIT', 'n_tokens_mistral': 985, 'n_tokens_neox': 960, 'n_words': 379, 'path': 'jupyter_notebooks/pandas/mastering_data_analysis/08. Regular Expressions/02. Quantifiers.ipynb', 'repo_name': 'manual123/Nacho-Jupyter-Notebooks', 'size': '3037'}