在python中使用正则表达式拆分字符串

时间:2018-05-02 13:10:47

标签: python regex

我试图将字符串分成两个字符串

def init_vsvars():
    cprint("")
    cprint_header("Initializing vs vars")

    if "cygwin" in platform.system().lower():
        vswhere_path = "${ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe"
    else:
        vswhere_path = r"%ProgramFiles(x86)%/Microsoft Visual Studio/Installer/vswhere.exe"
    vswhere_path = path.expandvars(vswhere_path)
    if not path.exists(vswhere_path):
        raise EnvironmentError("vswhere.exe not found at: %s", vswhere_path)

    vs_path = common.run_process(".", vswhere_path,
                                 ["-latest", "-property", "installationPath"])
    vs_path = vs_path.rstrip()

    vsvars_path = os.path.join(vs_path, "VC/Auxiliary/Build/vcvars64.bat")

    env_bat_file_path = "setup_build_environment_temp.bat"
    env_txt_file_path = "build_environment_temp.txt"
    with open(env_bat_file_path, "w") as env_bat_file:
        env_bat_file.write('call "%s"\n' % vsvars_path)
        env_bat_file.write("set > %s\n" % env_txt_file_path)

    os.system(env_bat_file_path)
    with open(env_txt_file_path, "r") as env_txt_file:
        lines = env_txt_file.read().splitlines()

    os.remove(env_bat_file_path)
    os.remove(env_txt_file_path)
    for line in lines:
        pair = line.split("=", 1)
        os.environ[pair[0]] = pair[1]

试过: 我尝试先删除最后一个字符" INPUT: "ASSO|ASSOCS|AS|ASSOCIATES/ASSOC/" OUTPUT: "ASSO|ASSOCS|AS|ASSOCIATES","ASSOC" ,然后用/"替换另一个字符。想知道我们可以使用正则表达式执行这两个步骤

","

1 个答案:

答案 0 :(得分:1)

如果您是"尝试将字符串拆分为两个字符串",那么您可以

s = "ASSO|ASSOCS|AS|ASSOCIATES/ASSOC/"
s1, s2, _ = s.split('/')