-2

I want to see that here :

[AA] - ( 371M 39F - COMPLETE ) - [AA]

but it always changes, for example it can be like this here

[AA] - ( 371M 39F - COMPLETE ) - [AA]
[cc] - ( 3M 3F - COMPLETE ) - [cc]
[1234] - ( 99.9M 111F - COMPLETE ) - [1234]
[A55bg] - ( 45571M 31239F - COMPLETE ) - [A55bg]

It always starts with [ and ends with ]

the following is always the same

[ ] - (  M  F - COMPLETE ) - [ ]

I would like to use the variable 'skip' to check whether it appears in the list.

import re

list = ['/home', '/home/test1', '/home/test1/aaa', '/home/test1/aaa/[AA] - ( 371M 39F - COMPLETE ) - [AA]', 'ccc']
skip = '\[([0-9|a-z|A-Z]+)\]\s+\-\s+\(\s+(\d+\.*\d*)M\s+(\d+)F\s+\-\s+COMPLETE\s+\)\s+\-\s+\[([0-9|a-z|A-Z]+)\]$'

for element in list:
    m = re.search(skip, element)
    if m:
        print('match')
kulle.alle
  • 67
  • 5
  • You are literally looking for `[zz] ...`. You should probably read about some regex tokens if you're going to use regex... For example, "any lower case letter" can be denoted as `[a-z]` – Tomerikoo Mar 17 '21 at 20:59
  • 1
    Does this answer your question? [What is the difference between re.search and re.match?](https://stackoverflow.com/questions/180986/what-is-the-difference-between-re-search-and-re-match) – Tomerikoo Mar 17 '21 at 21:03
  • it is also not found with re.search – kulle.alle Mar 17 '21 at 21:04
  • Because you have `^` and `$`. Do you know what they mean? – Tomerikoo Mar 17 '21 at 21:08
  • yes now i have understood it. and it works even if I remove the ^, thanks – kulle.alle Mar 17 '21 at 21:14
  • But now you have a working code in the question which makes it unclear. Please rollback your question to its original form. You can then post an answer to your own question if you solved it – Tomerikoo Mar 17 '21 at 21:16

1 Answers1

0

Assuming the characters strings noted by zz are alphanumeric only, you could use a Regex pattern like the following. It will emit a match group for each of the zz in the output list for your further use.

\[([0-9a-zA-Z]+)\]\s+\-\s+\(\s+(\d+\.*\d*)M\s+(\d+)F\s+\-\s+COMPLETE\s+\)\s+\-\s+\[([0-9a-zA-Z]+)\]

This does allow for any length of each zz 1 or longer, as well as multiple whitespaces wherever one appears. If you need to constrain it further, edit away!

Clay Roper
  • 86
  • 5