0

I have a file with some content as wells as the two 'include' strings below:

some string1
some string2
...

include: "base_pricing_app_model_my_deployment_id.model.lkml"
include: "base_map_layers.model.lkml"
...
some string3
...

Substring "my_deployment_id" is a token I need to add to the second 'include' string so file content looks like that:

some string1
some string2
...

include: "base_pricing_app_model_my_deployment_id.model.lkml"
include: "base_map_layers_my_deployment_id.model.lkml"
...
some string3

I am running Python 3.5 and using its re library. Here is how I attempt to replace the second 'include' string:

with fileinput.input(files=(rfi), inplace=True) as cmodf:
for line in cmodf:
    match_no_dep_id = re.sub('^include:\s\"(?!my_deployment_id).*[.]model[.]lkml\"$', line.split('.')[0] + '_' + 'my_deployment_id' + 'model.lkml' + '"',  line)
    print(match_no_dep_id, end='')

However, both 'include' entries get modified and I end up with the following content:

some string1
some string2

include: "base_pricing_app_model_my_deployment_id_my_deployment_id.model.lkml"
include: "base_map_layers_my_deployment_id.model.lkml"

some string3
...

So my negative lookahead regexp matches both strings, including the one which already contains the token I am negating.

I am not sure what I am missing here. Please help.

Thank you!

1 Answer 1

1

I believe what you want is ^include:\s\".*(?<!my_deployment_id)\.model\.lkml\"$

With yours (i.e. ^include:\s\"(?!my_deployment_id).*[.]model[.]lkml\"$), you are filtering out entries like these include: "include: "my_deployment_id.base_map_layers.model.lkml" (Notice my_deployment_id comes before and not right before .model.lkml)

In [1]: string1 = 'include: "base_pricing_app_model_my_deployment_id.model.lkml"'
   ...: string2 = 'include: "base_map_layers.model.lkml"'
   ...: 
   ...: import re
   ...: 
   ...: 
   ...: def clean_include(include: str) -> str:
   ...:     return re.sub(
   ...:         '^include:\s\".*(?<!my_deployment_id)\.model\.lkml\"$',
   ...:         include.split('.')[0] + '_' + 'my_deployment_id' + '.model.lkml' + '"',
   ...:         include,
   ...:     )
   ...: 
   ...: 
   ...: print(clean_include(string1))
   ...: print(clean_include(string2))
   ...: 
   ...: 
include: "base_pricing_app_model_my_deployment_id.model.lkml"
include: "base_map_layers_my_deployment_id.model.lkml"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.