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

Add support for list of regex in regex.json #244

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions pywhat/Data/regex.json
Expand Up @@ -1591,6 +1591,33 @@
"Invalid": []
}
},
{
"Name": "Phone Number",
"Regex": "^[0-9]([\\.\\s\\-]?\\d){6}$",
"plural_name": false,
"Description": null,
"Rarity": 0.5,
"URL": null,
"Tags": [
"Identifiers",
"Credentials",
"Phone Number",
"Phone",
"Telephone"
],
"Children": {
"path": "phone_codes.json",
"entry": "Location(s): ",
"method": "hashmap"
},
"Examples": {
"Valid": [
"1234567",
"7777777"
],
"Invalid": []
}
},
{
"Name": "American Social Security Number",
"Regex": "^(([0-9][0-9][0-9])(?<!666|000|9[0-9][0-9])(=|\\+|_|\\#|:|;|\\.|-|\\s){0,3}([1-9][1-9]|0[1-9]|[1-9]0)(=|\\+|_|\\#|:|;|\\.|-|\\s){0,3}([0-9][0-9][0-9][0-9])(?<!0000)(=|\\+|_|\\#|:|;|\\.|-|\\s){0,3}?)$",
Expand Down
33 changes: 31 additions & 2 deletions pywhat/helper.py
Expand Up @@ -40,6 +40,8 @@ def read_json(path: str):

@lru_cache()
def load_regexes() -> list:
combined_regexes = []
regex_index_mapping = {}
regexes = read_json("regex.json")
for regex in regexes:
regex["Boundaryless Regex"] = re.sub(
Expand All @@ -57,8 +59,35 @@ def load_regexes() -> list:
children["lengths"] = set()
for element in children["Items"]:
children["lengths"].add(len(element))
return regexes

# Check if multiple regex patterns present
regex_name = regex["Name"]
if(regex_name in regex_index_mapping):
# Update regex pattern
existing_regex = combined_regexes[regex_index_mapping[regex_name]]["Regex"]
updated_regex = existing_regex + "|" + regex["Regex"]
combined_regexes[regex_index_mapping[regex_name]]["Regex"] = updated_regex
# Combine tags
updated_tags = combined_regexes[regex_index_mapping[regex_name]]["Tags"]
for tag in regex["Tags"]:
if(tag not in updated_tags):
updated_tags.append(tag)
# Combine examples
updated_valid_examples = combined_regexes[regex_index_mapping[regex_name]]["Examples"]["Valid"]
for valid_example in regex["Examples"]["Valid"]:
if(valid_example not in updated_valid_examples):
updated_valid_examples.append(valid_example)
updated_invalid_examples = combined_regexes[regex_index_mapping[regex_name]]["Examples"]["Invalid"]
for invalid_example in regex["Examples"]["Invalid"]:
if(invalid_example not in updated_invalid_examples):
updated_invalid_examples.append(invalid_example)
# Update boundaryless regex pattern
existing_boundaryless_regex = combined_regexes[regex_index_mapping[regex_name]]["Boundaryless Regex"]
updated_boundaryless_regex = existing_boundaryless_regex + "|" + regex["Boundaryless Regex"]
combined_regexes[regex_index_mapping[regex_name]]["Boundaryless Regex"] = updated_boundaryless_regex
else:
regex_index_mapping[regex_name] = len(combined_regexes)
combined_regexes.append(regex)
return combined_regexes

class CaseInsensitiveSet(collections.abc.Set):
def __init__(self, iterable=None):
Expand Down