RegEx: Is there a way to extract all substrings wrapped in between single quotation marks without extracting substrings in between apostrophes?

Apologies if the title is not clear—this is my first post on stack overflow. I will try to provide as much context as possible.

Basically, I have a dataset containing english statements. Some of the statements contain leading/trailing punctuation, so I would like to clean and standardize them. The criteria for standardization is that all leading/trailing punctuation is removed, the first character of the statement is capitalized, and the statement ends in a period. I have a function that does this and apply it to each row in the dataset. Here is a snippet if it helps:

def normalize_statement(statement):
    # remove leading/trailing punctuation and excess whitespace
    cleaned = statement.strip(whitespace + punctuation)
    cleaned = re.sub(r's+', ' ', cleaned)

    # capitalize first letter 
    cleaned = cleaned[0].upper() + cleaned[1:]

    # ensure the statement ends with a period
    if cleaned and not cleaned.endswith('.'):
            cleaned += '.'
    return cleaned

This function works for basic statements, but doesn’t always work with substrings wrapped between double or single quotation marks, especially if the quoted substring appears in the beginning and/or end of the statement because then one of the quotation marks gets removed after leading punctuation is removed (ie. ‘”this” is a statement’ would become ‘This” is a statement’). The intent should be to preserve the quotation marks around any substring after cleaning.

I thought about using regex to capture all substrings in between single or double quotation marks, replace all quoted substrings with the captured groups to remove the quotation marks, standardize the statement, then add back the quoted substrings in their original positions. Here is the updated function:

def normalize_statement(statement):
    # extract any and all substrings enclosed in quotation marks
    quoted_pattern = re.compile(r'["'](.*?)["']')
    quoted_substrings = quoted_pattern.findall(statement)

    # replace the quoted substrings with the captured groups
    cleaned = re.sub(quoted_pattern, lambda m: m.group(1), statement)

    # remove leading/trailing punctuation and excess whitespace
    cleaned = cleaned.strip(whitespace + punctuation)
    cleaned = re.sub(r's+', ' ', cleaned)

    # capitalize first letter for languages that use capitalization
    cleaned = cleaned[0].upper() + cleaned[1:]

    # ensure the statement ends with a period
    if cleaned and not cleaned.endswith('.'):
            cleaned += '.'

    # replace quoted substrings (if any) back into their original positions after cleaning
    for quoted_substring in quoted_substrings:
        cleaned = re.sub(re.escape(quoted_substring.strip(whitespace + punctuation)), f'"{quoted_substring}"', cleaned, flags=re.IGNORECASE)

    return cleaned

While the regex pattern ‘”‘[“‘]’ works sometimes (ie. Employees are “expected” to embrace ‘frugality’.), it fails for statements like ‘There’s no place like home, emphasizing the unique comfort of one’s own surroundings.’ because the captured group in this case would be the substring between the apostrophes: ‘s no place like home, emphasizing the unique comfort of one’. I only want text between quotations to be captured—but how can I differentiate between apostrophes and single quotes in my regex pattern? I’m not too familiar with regex, so if anyone has a different approach, that would be greatly appreciated!

New contributor

Dan Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật