Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 630 Bytes

extracting-substring.md

File metadata and controls

25 lines (17 loc) · 630 Bytes

Extracting a substring with ReGex

I was tasked with extracting an advertiser name from a string. I came across this solution which seems to work very well using regex.

Using regular expressions - documentation for further reference

import re

some_string = "2022_PULSEPOI_COH_PHRM_DAILY_REPORT"


def get_advertiser_name(string):
    if advertiser := re.search(
        "2022_PULSEPOI_(.+?)_PHRM_DAILY_REPORT", string
    ):
        return advertiser[1]


# returns COH