Difference Between the patterns - Counting Mentions of the 'C' Language

def first_10_matches(pattern):
    """
    Return the first 10 story titles that match
    the provided regular expression
    """
    all_matches = titles[titles.str.contains(pattern)]
    first_10 = all_matches.head(10)
    return first_10

pattern1 = r"\b[Cc]\b[^.+]"
 pattern2 = r"\b[Cc]\b[^+.]"

first_10 = first_10_matches(pattern)

You can see the two patterns which follow different results. Can someone explain me the difference between them. I know that + is a special char that means 1 or more times of the previous char in regex. But I didn’t understand how the python algo runs it?
Why it works in pattern1 but not in pattern 2?

hi @npekceti

Please utilize this guideline to attach a mission link and mission tag to the question raised.
This helps the community to provide a response more efficiently.

You have created two variables pattern1 & pattern2, but you are passing the variable "pattern". Or you haven’t attached the codes for them.

Here + and . are taking their regex definitions:
“.” - any character except a newline
“+” - a character’s occurrence one or more times

In order to treat them as string characters i.e. a period(.) and a plus symbol(+) you need to use escape character "\" in the search pattern.

1 Like