Package: regexp-sar
SAR - Simple Api for RegexpRegexp SAR - a python module for multi match event handling regular expression engineDescriptionSAR is a new way of handling regular expression which allows us to run many regular expressions (only limitation being the available memory) at once.
When adding a regexp, there is also a related callback that will be called upon each match in the same order in which they appear on the textInstallBefore installation, make sure you have the latest version of pip:pipinstall--upgradepipInstall regexp-sar:pipinstallregexp-sarImportfromregexp_sarimportRegexpSarExample'''This example will find and print second match of each regexp,while also showing what regexp was caught'''fromregexp_sarimportRegexpSarsar=RegexpSar()# string to be matched againstmatch_str="hello world 123 abc 456 789"# list of regexps, first item in pair is the regexp,# second item in the pair is a unique word for that regexpregexps=[['\w+','word'],['\d+','number'],]# add all regexps in a loopforcur_regexpinregexps:deffind_second_match(description):match_count=0match_val=None# define inner method, to use with closuredefcallback(from_pos,to_pos):nonlocalmatch_count,match_valmatch_count+=1ifmatch_count==2:print("Match: "+str(description)+": "+match_str[from_pos:to_pos])sar.continue_from(to_pos)returncallback# add regexp with a callbacksar.add_regexp(cur_regexp[0],find_second_match(cur_regexp[1]))# run matchsar.match(match_str)'''Output:Match: word: worldMatch: number: 456'''Methodsconstructorcreates a new sar instance with its own regexps and callbacks, many instances can be built at onceadd_regexpadds a regexp into the sar instance, recieved 2 parameters:regexp - the required regexpcallback - the callback which will be called upon match, the callback receives 2 parameters -from_pos - the start position of the match in the matched stringto_pos   - the end position of the match in the matched string (exclude to_pos)sar=RegexpSar()sar.add_regexp('abc',lambdafrom_pos,to_pos:print("Match: "+str(from_pos)+"->"+str(to_pos)))sar.match("hello abc world")# Match: 6->9matchbegins a match against the previously defined regexps on the received string.
receive 1 parameter:string to be matched withNOTE: this is syntactic sugar formatch_from(match_str, 0)match_fromacts likematchbut starts from a custom position the search.
receive 2 parameters:string to be matched withstart position of the matchmatch_atlooks for a match from a specific character only, and will not continue to search for matches in the following characterscontinue_fromCan be called only during a match/match_from, will continue the next match character from the given character indexreceive 1 parameter:position for next matchstop_matchCan be called only during a match/match_from, will stop the match after current char matching has endedrulesabbreviations.  - matches any character\d - matches a digit character (checked by isdigit method)\w - matches an alphanumeric character (checked by isalnum method)\a - matches an alpha character (checked by isalpha method)\s - matches a space character (checked by isspace method)^ - matches a characterNOTfollowed by the match (i.e.\^\d+will match all non digit strings)repetition'?' - matches 1 or 0 times'*' - matches 0 or more times'+' - matches 1 or more timesbackslash ('\') characterin order to match the '\' character, it needed to be followed by 3 more backslashes (4 in total) since python string takes 2 backslashes to be represented as onesar=RegexpSar()sar.add_regexp('\\\\',lambdafrom_pos,to_pos:print("Match: "+str(from_pos)+"->"+str(to_pos)))sar.match('a\\b')# Match: 1->2examplesExamples may be found in thetest_oousage.pyfile, and in the examples directoryArticlesFor more information, visitmy introduction postFor practical example, visitPractical example where SAR comes to play.For my article regarding incorporating SAR into DLP (Data Leak Prevention), visitDLP (Data Loss Prevention) in SARUnicode supportCurrently not supported. May be added in future updateAuthorNoam Nisanov -noam.nisanov@gmail.com