Return all strings matching 'pattern' (a regex or callable) This is case-insensitive. If prune is true, return all items NOT matching the pattern. If field is specified, the match must occur in the specified whitespace-separated field. Examples::
(self, pattern, prune = False, field = None)
| 130 | p = paths = property(get_paths) |
| 131 | |
| 132 | def grep(self, pattern, prune = False, field = None): |
| 133 | """ Return all strings matching 'pattern' (a regex or callable) |
| 134 | |
| 135 | This is case-insensitive. If prune is true, return all items |
| 136 | NOT matching the pattern. |
| 137 | |
| 138 | If field is specified, the match must occur in the specified |
| 139 | whitespace-separated field. |
| 140 | |
| 141 | Examples:: |
| 142 | |
| 143 | a.grep( lambda x: x.startswith('C') ) |
| 144 | a.grep('Cha.*log', prune=1) |
| 145 | a.grep('chm', field=-1) |
| 146 | """ |
| 147 | |
| 148 | def match_target(s): |
| 149 | if field is None: |
| 150 | return s |
| 151 | parts = s.split() |
| 152 | try: |
| 153 | tgt = parts[field] |
| 154 | return tgt |
| 155 | except IndexError: |
| 156 | return "" |
| 157 | |
| 158 | if isinstance(pattern, str): |
| 159 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
| 160 | else: |
| 161 | pred = pattern |
| 162 | if not prune: |
| 163 | return SList([el for el in self if pred(match_target(el))]) |
| 164 | else: |
| 165 | return SList([el for el in self if not pred(match_target(el))]) |
| 166 | |
| 167 | def fields(self, *fields): |
| 168 | """ Collect whitespace-separated fields from string list |