0

I have a path that I have assigned to a string:

string = 'pathos/llb_cube/uni/ToolSub.pm'

However the 'llb' prefix can be different string for each computer and I need to assign it to an arbitrary value that will read whatever the user's computer has set for this specific directory such as:

string = 'pathos/*_cube/uni/ToolSub.pm'

I cannot figure out what function or regex to use for this however.

2
  • "read whatever the user's computer has set for this specific directory" -- what does this mean? Have you solved this part yourself? Commented Sep 4, 2018 at 20:52
  • Have a look at pathlib which supports globs of this type. Better suited than a regex. Commented Sep 4, 2018 at 21:18

1 Answer 1

1

You can use the dot . to mean 'any character':

string = r'pathos/.*_cube/uni/ToolSub\.pm'
  • I escaped the dot at the end of the string.

  • The * asterisk denotes 0 or more repetitions.

  • I used a raw string literal: r'...' so that I can write backslashes without having to escape them.

If you need a certain number of characters use .{x}. Now you can use regex to match your paths

for path in path_list:
    match = re.match(string, path)
    if match:
        print(match.group(0))
Sign up to request clarification or add additional context in comments.

1 Comment

So the program is returning the string as simply just pathos/.*_cube/uni/ToolSub\.pm rather than recognizing '.*' to be a regex. :( Therefore when I try to check if the file path exists it is not recognizing because it is reading '.*_cube'

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.