0

I have a

string = 'long company name with technologies in it'

and want to replace all tokens starting with

search_string ='techno'

with a new token

replace_string = 'tech'.

I wrote a function:

def group_tokens(company_name, string_search, string_replace):   
try:
    x = company_name.split(" ") 
    print(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    exec(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    x = " ".join(x)
    x = " ".join(re.split("\s+", x, flags=re.UNICODE))
    return(x)
except:
        return np.nan

If I execute the lines separately it works. But the function itself doesn't work.

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with technologies in it'

I'd expect

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with tech in it'

How can I "exec" f-string in a function?

Judy
  • 35
  • 5
  • 2
    You don't. Why do you need an f-string? Simply re-assign x: `x = [re.sub(f'^{string_search}.*', …) for i in x]` – knittl Sep 24 '22 at 17:54
  • 1
    `re.sub()` replaces all matching substrings anyway. You don't need a loop or comprehension – rdas Sep 24 '22 at 17:55

3 Answers3

2

You are overcomplicating this. Simply reassign x:

def group_tokens(company_name, string_search, string_replace):   
  try:
    x = company_name.split(" ") 
    x = [re.sub(f'^{string_search}.*', string_replace, i) for i in x])
    x = " ".join(x)
    x = " ".join(re.split("\s+", x, flags=re.UNICODE))
    return x
  except:
    return np.nan

But it's probably easier to rewrite the function similar to the following:

def group_tokens(company_name, string_search, string_replace):   
  return re.sub(f'\b{string_search}\S*\s*', f'{string_replace} ', company_name, flags=re.UNICODE);
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks for the edit (`^` -> `\b^` was correct), but `\S*\s*` cannot be replaced with `\w*` because the existing code collapses multiple whitespace characters into one – knittl Sep 25 '22 at 06:40
0
def replace(string,x,y):
    words = string.split(' ')
    string = ''
    while words:
        word = words.pop(0)
        if word.startswith(x):
            word = y
        string+=word+' '
    return string[:-1]

print(replace('long company name with technologies in it', 'techno', 'tech'))

    
islam abdelmoumen
  • 662
  • 1
  • 3
  • 9
0

I definitely overcomplicated it. Thanks :-)

def group_tokens(company_name, string_search, string_replace):   
  try:
    x = company_name.split(" ") 
    x = [re.sub(f'^{string_search}.*', string_replace, i) for i in x])
    x = " ".join(x)
    x = " ".join(re.split("\s+", x, flags=re.UNICODE))
    return x
  except:
   return np.nan
Judy
  • 35
  • 5