From d896dd86e766e6d45a064b7625dce01e26307419 Mon Sep 17 00:00:00 2001 From: GauriBodke Date: Sat, 11 Dec 2021 16:31:41 -0500 Subject: [PATCH] Add support for list of regex in regex.json --- pywhat/Data/regex.json | 27 +++++++++++++++++++++++++++ pywhat/helper.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/pywhat/Data/regex.json b/pywhat/Data/regex.json index eaf8fff..e8478f0 100644 --- a/pywhat/Data/regex.json +++ b/pywhat/Data/regex.json @@ -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])(? list: + combined_regexes = [] + regex_index_mapping = {} regexes = read_json("regex.json") for regex in regexes: regex["Boundaryless Regex"] = re.sub( @@ -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):