Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex._regex_core.error: bad escape \w at position 8 #1161

Open
captainIT opened this issue May 24, 2023 · 6 comments
Open

regex._regex_core.error: bad escape \w at position 8 #1161

captainIT opened this issue May 24, 2023 · 6 comments
Assignees

Comments

@captainIT
Copy link

          **在 Windows  环境中 如果有个环境变量包含windows路径,如下:**
import regex as re
regexp = '\\{\\{[\\\'\\"]?\\s*\\$WORK_DIR\\s*\\:\\s*None\\s*[\\\'\\"]?\\}\\}'
value = "D:\\666\\work_dir"
config_file='_work_dir = {{"$DLP_WORK_DIR:None"}}'
config_file = re.sub(regexp, value, config_file)

返回错误
regex._regex_core.error: bad escape \w at position 8

Originally posted by @captainIT in #744 (comment)

@mm-assistant
Copy link

mm-assistant bot commented May 24, 2023

We recommend using English or English & Chinese for issues so that we could have broader discussion.

@HAOCHENYE
Copy link
Collaborator

Will replace value with rf'{value}' help you?

@captainIT
Copy link
Author

value 是通过环境变量设置的字符串

代码来源如下
mmengine\config\config.py

 @staticmethod
def _substitute_env_variables(filename: str, temp_config_name: str):
      with open(filename, encoding='utf-8') as f:
            config_file = f.read()
        regexp = r'\{\{[\'\"]?\s*\$(\w+)\s*\:\s*(\S*?)\s*[\'\"]?\}\}'
        keys = re.findall(regexp, config_file)
        env_variables = dict()
        for var_name, value in keys:
            regexp = r'\{\{[\'\"]?\s*\$' + var_name + r'\s*\:\s*' \
                + value + r'\s*[\'\"]?\}\}'
            if var_name in os.environ:
                value = os.environ[var_name]
                env_variables[var_name] = value
                print_log(
                    f'Using env variable `{var_name}` with value of '
                    f'{value} to replace item in config.',
                    logger='current')
            if not value:
                raise KeyError(f'`{var_name}` cannot be found in `os.environ`.'
                               f' Please set `{var_name}` in environment or '
                               'give a default value.')
            config_file = re.sub(regexp, value, config_file)

        with open(temp_config_name, 'w', encoding='utf-8') as tmp_config_file:
            tmp_config_file.write(config_file)
        return env_variables

@HAOCHENYE
Copy link
Collaborator

HAOCHENYE commented Jun 1, 2023

I mean you can try to replace value with rf'{value}' in this line

regexp = r'\{\{[\'\"]?\s*\$' + var_name + r'\s*\:\s*' \

If it helps, we could enhance this code snippet ASAP

@captainIT
Copy link
Author

import regex as re
regexp = '\\{\\{[\\\'\\"]?\\s*\\$DLP_WORK_DIR\\s*\\:\\s*None\\s*[\\\'\\"]?\\}\\}'
value1 = rf"D:\\666\\work_dir"
value2 = "D:\\666\\work_dir"
value2 = rf'{value2}'
config_file='_work_dir = {{"$DLP_WORK_DIR:None"}}'
config_file1 = re.sub(regexp, value1, config_file)
print(f"========config_file1:===:{config_file1}:===========")
config_file2 = re.sub(regexp, value2, config_file)
print(f"========config_file2:===:{config_file2}:===========")

第一个可以正常输出
========config_file1:===:_work_dir = D:\666\work_dir:===========
第二个报错 regex._regex_core.error: bad escape \w at position 8

@HAOCHENYE
Copy link
Collaborator

Emmm, maybe value = value.encode('unicode_escape').decode() could help, but we need to study whether this modification is general enough for all platforms.

@zhouzaida zhouzaida changed the title **在 Windows 环境中 如果有个环境变量包含windows路径,如下:** regex._regex_core.error: bad escape \w at position 8 Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants