From the following list of filenames I am trying to retrieve the highlighted parts:
The pattern is:
import os
import re
root = "C:/root"
data = dict()
re_pattern = "[a-zA-Z|2-9][h|s|c|d][a-zA-Z|2-9][h|s|c|d][a-zA-Z|2-9][h|s|c|d]"
for folder in os.listdir(root):
data[folder] = dict()
for item in os.listdir(f"{root}/{folder}"):
board_id = re.findall(item, re_pattern)
print(board_id)
data[folder][item] = f"{root}/{folder}/{item}"
I thought my regex would work but it finds an empty list. Is my regex or my code wrong? The goal is to have the board_id be the dictionary key and the value the entire path.
EDIT Improved pattern looks like:
import os
import re
root = "C:/root"
data = dict()
re_pattern = "(?i)(?:[2-9AJKQT][hscd]){3}"
for folder in os.listdir(root):
data[folder] = dict()
for item in os.listdir(f"{root}/{folder}"):
board_id = re.search(item, re_pattern)
print(f"{item} :: {board_id}")
data[folder][item] = f"{root}/{folder}/{item}"
Results are still not right, must be in the code instead:
Best regards
The expression \w will match any word character. Word characters include alphanumeric characters ( - , - and - ) and underscores (_). \W matches any non-word character.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters.
To find all the matching strings, use String's scan method.
How about using character classes in a quantified group.
(?i)(?:[2-9AJKQT][hscd]){3}
See this demo at regex101 or this Python demo
For caseless matching use (?i)
flag or re.IGNORECASE
.
Taking a closer look at your code, further be aware of order, arguments are passed in re.findall
re.findall(pattern, string, flags=0)
Another idea for future, it might further be, to consider generally using raw string notation for regex patterns but this is not an issue with your current pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With