0

the string is 'POSIX TAR ARCHIVE (GNU)' the regex pattern '(TAR)|(POSIX)'

python code :

import re 
pattern = '(TAR)|(POSIX)'
string = 'POSIX TAR ARCHIVE (GNU)'
match = re.search(pattern, string , re.IGNORECASE)
match.groups()

the result is only POSIX not tar why??

gedalia
  • 3
  • 3
  • 4
    Use `re.findall` to get multiple matches. And use `'TAR|POSIX'`. `print(re.findall(r'TAR|POSIX', s))` – Wiktor Stribiżew May 02 '18 at 11:32
  • 1
    Dupe of [How can I find all matches to a regular expression in Python?](http://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python). – Wiktor Stribiżew May 02 '18 at 11:33
  • 1
    Possible duplicate of [How can I find all matches to a regular expression in Python?](https://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python) – Uyghur Lives Matter May 02 '18 at 13:55

1 Answers1

0

try this:

re.findall(pattern, string, re.IGNORECASE)

re.findall matches all the patterns you are looking for,
while re.search finds and returns the first match from the string.

AverageJoe9000
  • 348
  • 1
  • 14