Package lib :: Module regex
[hide private]
[frames] | no frames]

Source Code for Module lib.regex

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2003-2012 Edward d'Auvergne                                   # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Module implementing relax regular expression.""" 
24   
25  # Python module imports. 
26  import re 
27   
28   
29 -def search(pattern, id):
30 """Determine if id matches the pattern, or vice versa, allowing for regular expressions. 31 32 This method converts from relax's RE syntax to that of the re python module. 33 34 The changes include: 35 36 1. All '*' to '.*'. 37 2. The identifier is bracketed, '^' is added to the start and '$' to the end. 38 39 After conversion of both the string and patterns, the comparison is then performed both ways from the converted string matching the original string (using re.search()). 40 41 42 @param pattern: The pattern to match the string to. This can be a list of patterns. All elements will be converted to strings, so the pattern or list can consist of anything. 43 @type pattern: anything 44 @param id: The identification object. 45 @type id: None, str, or number 46 @return: True if there is a match, False otherwise. 47 @rtype: bool 48 """ 49 50 # Catch None. 51 if id == None: 52 return False 53 54 # Convert to a string. 55 id = str(id) 56 57 # If pattern is not a list, convert it to one. 58 if not isinstance(pattern, list): 59 patterns = [pattern] 60 else: 61 patterns = pattern 62 63 # Loop over the patterns. 64 for pattern in patterns: 65 # Force a conversion to str. 66 pattern = str(pattern) 67 68 # Quick string check. 69 if id == pattern: 70 return True 71 72 # First replace any '*' with '.*' (relax to re conversion). 73 pattern_re = pattern.replace('*', '.*') 74 id_re = id.replace('*', '.*') 75 76 # Bracket the pattern. 77 pattern_re = '^%s$' % pattern_re 78 id_re = '^%s$' % id_re 79 80 # String matches (both ways). 81 if re.search(pattern_re, id): 82 return True 83 if re.search(id_re, pattern): 84 return True 85 86 # No matches. 87 return False
88