Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:55:13+00:00 2026-06-02T23:55:13+00:00

I have a regex to match middle names that looks like this : first_name

  • 0

I have a regex to match middle names that looks like this :

first_name = 'Matthew'
last_name = 'Walsh'
for char in first_name:
    new_first_name+='(' + char.lower() + '|' + char.upper() + ')'
for char in last_name:
    new_last_name+='(' + char.lower() + '|' + char.upper() + ')'


middle_name_regex_str = "\b?((" + new_first_name + " (?P<middle_name1>[A-Z][^ ]?[a-z]* )?" + new_last_name + ")|(" + new_last_name + " (?P<middle_name2>[A-Z][^ ]?[a-z]* )?" + new_first_name + "))"

here is a pattern it works for :

MATTHEW B. WALSH, D.M.D.\nBorn:\nAkron, Ohio\nCollege:\nBachelor of
Arts, Kenyon College, 1998

for this pattern it works fine, and matches the middle name ‘B.’

however I want to be on the safe side and escape the first and last names, but when I add re.escape it fails :

middle_name_regex_str = "\b?((" + re.escape(new_first_name) + " (?P<middle_name1>[A-Z][^ ]?[a-z]* )?" + re.escape(new_last_name) + ")|(" + re.escape(new_last_name) + " (?P<middle_name2>[A-Z][^ ]?[a-z]* )?" + re.escape(new_first_name) + "))"

and now the regex does not match properly :

regex = re.compile(middle_name_regex_str)
regex.search('MATTHEW B. WALSH, D.M.D.\nBorn:\nAkron, Ohio\nCollege:\nBachelor of Arts, Kenyon College, 1998')

this returns nothing.

Shouldn’t re.escape be safe to use in the sense that it does not alter the behavior of the expression? What in adding backslashes before non alphanumeric chars could cause it to not match?

Any help would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:55:14+00:00Added an answer on June 2, 2026 at 11:55 pm

    using re.escape on something that already contains regex special chars will look for those literal chars.

    Two suggestions here:

    1. if possible, why not use re.IGNORECASE to test the regex regardless of case?

    2. if not, you can do something like this

    first_name= 'Matthew'

    last_name = 'Walsh'
    
    first_name_re = "".join('(%s|%s)' % (re.escape(c.upper()),re.escape(c.lower())) for c in first_name)
    last_name_re = "".join('(%s|%s)' % (re.escape(c.upper()),re.escape(c.lower())) for c in last_name)
    
    
    # now that they are safe -we can simply put them in the middle of the regex
    middle_name_regex_str = "\b?((%s (?P<middle_name1>[A-Z][^ ]?[a-z]* )?%s)|(%s (?P<middle_name2>[A-Z][^ ]?[a-z]* )?%s))" % (first_name_re, last_name_re, first_name_re, last_name_re) 
    

    not sure about the order of formatting args here,but you get the point

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a RegEx that will match a URI like this based
I'm using C# and .NET and I have a Regex that looks like this
I have this regex that is supposed to match 1='aa' or 1=aa or 1=aa,
I have this regex that detects hashtags. It shouldn't match things with letters before
I have the following data: abc def; ghi. This regex will match: ([a-z0-9A-ZÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ ]*)\W
Is this Regex correct if I have to match a string which is atleast
^(.)+\S{10}(.)+$ I have that regex which will match any string that contains a word
I have this code Regex.Match(contents, @Security=(?<Security>\D+)).Groups[Security].Value; this makes the following: Security=SSPI;Database=Datab_ob How do I
I would like to match dotall and non-greedy. This is what I have: img(.*?)(onmouseover)+?(.*?)a
I have a regex I need to match against a path like so: C:\Documents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.