user92592 user92592 - 1 year ago 57
Python Question

Trying to understand Python Regex's

I am trying to write a Python regex to capture the full name of someone whose last name is Nakamoto? You can assume that the first name that comes before it will always be one word that begins with a capital letter. The regex must match the following:

'Satoshi Nakamoto'
'Alice Nakamoto'
'RoboCop Nakamoto'

but not the following:

'satoshi Nakamoto' (where the first name is not capitalised)
'Mr. Nakamoto' (where the preceding word has a nonletter character)
'Nakamoto' (which has no first name)
'Satoshi nakamoto' (where Nakamoto is not capitalised)

I have used the following regex:

However this captures both
Satoshi Nakamoto
satoshi Nakamoto
. I would like to understand where I am going wrong and how to correct it. Here is my code:

import re #import regular expressions

#regular expression
NameSearch = re.compile(r'[A-Z][a-z]+\sNakamoto', re.I | re.VERBOSE)

# perform search on string
Result ="Satoshi Nakamoto")

#Debug code to check if it found a match or not
print (Result == None)

if Result != None:
print (

Answer Source

Your regular expression actually works fine over here but it will not match the "RoboCop Nakamoto" case.

import re

def printMatch(name):
    pat = re.compile(r'\b[A-Z][a-zA-Z]+\sNakamoto')
        print '"'+name+'" matches'
        print '"'+name+'" does not match'

printMatch('test satoshi Nakamoto test')
printMatch('test Satoshi Nakamoto test')
printMatch('test RoboCop Nakamoto test')
printMatch('test roboCop Nakamoto test')

The output is this:

"test satoshi Nakamoto test" does not match
"test Satoshi Nakamoto test" matches
"test RoboCop Nakamoto test" matches
"test roboCop Nakamoto test" does not match
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download