0

I can't pass this code challenge:

Regular Expression Search Challenge Using the Python string below to perform a search using a regular expression that you create.

search_string=’’’This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression’’’

Write a regular expression that will find all occurrences of: a. regular expression b. regular-expression c. regular:expression d. regular&expression in search_string

Assign the regular expression to a variable named pattern

Using the findall() method from the re package determine if there are occurrences in search_string

Assign the outcome of the findall() method to a variable called match1

If match1 is not None: a. Print to the console the pattern used to perform the match, followed by the word ‘matched’

Otherwise: a. Print to the console the pattern used to perform the match, followed by the words ‘did not match’

Here is my code:

import re
#The string to search for the regular expression occurrence (This is provided to the student)

search_string = '''This is a string to search for a regular expression like regular expression or 
regular-expression or regular:expression or regular&expression'''

#1.  Write a regular expression that will find all occurrences of:
#    a.  regular expression
#    b.  regular-expression
#    c.  regular:expression
#    d.  regular&expression
#    in search_string
#2.  Assign the regular expression to a variable named pattern
ex1 = re.search('regular expression', search_string)
ex2 = re.search('regular-expression', search_string)
ex3 = re.search('regular:expression', search_string)
ex4 = re.search('regular&expression', search_string)
pattern = ex1 + ex2 + ex3 + ex4
#1.  Using the findall() method from the re package determine if there are occurrences in search_string
#.   Assign the outcome of the findall() method to a variable called match1
#2.  If match1 is not None:
#    a.  Print to the console the pattern used to perform the match, followed by the word 'matched'
#3.  Otherwise:
#    a.  Print to the console the pattern used to perform the match, followed by the words 'did not match'
match1 = re.findall(pattern, search_string)
if match1 != None:
  print(pattern + 'matched')
else:
  print(pattern + 'did not match')

I don't really get any feedback from the program. It just tells me I failed without an error message.

1
  • 1
    You're too focused on solving the Challenge, honestly. I'll give you a hint with parts. 1. re.findall('ONE[\S]TWO', 'ONE&TWO'), 2. re.findall('ONE[\S]TWO', 'ONE)TWOTHREE'), 3. re.findall('ONE[\S]TWO', 'ONE)TWO----THREE'), 4. re.findall('ONE[\S]+TWO', 'ONE@TWO'), 5. re.findall('ONE[\S]+TWO', 'ONE>>>>>>TWO'), 6. re.findall('ONE[\S]+TWO', 'ONE>>>>>>TWO>>>>>>>THREE') Commented Apr 13, 2019 at 1:11

3 Answers 3

3

If I run your code I do get an error telling me that

pattern = ex1 + ex2 + ex3 + ex4

failed because adding Match objects is not supported.

The challenge is likely attempting to teach you to use character sets in regular expressions. Basically, you don't need ex1, ex2, and so on. You simply need to define the regex pattern in the pattern variable and supply it to re.findall.

I'd also recommend tools like RegExr and regex101 for experimenting with regex.

Sign up to request clarification or add additional context in comments.

2 Comments

So I changed my variable to pattern = re.findall('regular.expression', search_string) which returns the proper matches, but I still can't quite get it. Maybe it's the wording that's throwing me off, but why would I then need to assign the results of the pattern findall function to a new variable called match1?
I think you misunderstood, my point was that you should set pattern = 'some-regex-pattern-here'. Be aware that . in RegEx means "any character", so your current pattern will indeed match the examples, but also regular@expression, regularqexpression and so on, which is also why I pointed you towards character sets.
2

Hey there I know this is a bit late but I am currently going through these same challenges so I figured I'd lend a hand on how to get past the check point.

So "tfw" is absolutely correct in using the character sets. And its actually just 1 line of code for all of the...

#1.  Write a regular expression that will find all occurrences of:
#    a.  regular expression
#    b.  regular-expression
#    c.  regular:expression
#    d.  regular&expression
#    in search_string
#2.  Assign the regular expression to a variable named pattern

So basically the easiest way to do this is by specifying the expression with each character set. So it looks something like this...

pattern = "regular[ -:&]expression"

Doing this allowed us to add the space, the -, the : and the & in the same variable called pattern. Now your second part...

#1.  Using the findall() method from the re package determine if there are occurrences in search_string
#.   Assign the outcome of the findall() method to a variable called match1
#2.  If match1 is not None:
#    a.  Print to the console the pattern used to perform the match, followed by the word 'matched'
#3.  Otherwise:
#    a.  Print to the console the pattern used to perform the match, followed by the words 'did not match'

You already had the right code for, just needed the right regex to call from. But I'll post it just so no confusion.

match1 = re.findall(pattern, search_string)
if match1 != None:
  print(pattern + 'matched')
else:
  print(pattern + 'did not match')

Anyways hope this helps anyone else that may need just a little push in the right direction. I always find it easier to see what the code is doing rather than be told how the code should be with little direction :D

Comments

-1

Change the expression to pattern = regular[ -:&]expression

1 Comment

It seems duplicated with @RobertB922's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.