I have a string list where every line is like this:
1.alfa.america.game
i need to query this line with different parameters, and if matches, print it. with this example, i get all the lines which have "1" and "db", but also get others, ie:
11.alfa.cad.base
i don't want to get lines with "11" nor "db", only the exact match. this is what i did:
code:
ID = "1"
task = "db"
environment = "a-z"
location = "a-z"
fullString = "1.alfa.america.game" #this string can change
q = re.compile(r'(['+ID+'])+.(['+task+'])+.(['+environment+'])+.(['+location+'])+.', flags=re.I | re.X)
m = q.match(fullString)
if m:
print (fullString)
thanks in advance!
A few notes about the pattern, some of which are already mentioned in the comment.
With the current variables, the pattern would be
([1])+.([db])+.([a-z])+.([a-z])+.
.
matches any character instead of a dot only.()+
would capture the value of the last iteration, you want the group value as a whole so you can repeat the character class instead1
and db
are hardcoded, you don't really have to capture themTaking that into account, you could use 2 capturing groups instead. As you are using re.match you can omit the anchor at the start and assert the end of the string using \Z
1\.db\.([a-z]+)+\.([a-z]+)\Z
^ ^ ^
Dot group 1 group 2
q = re.compile(ID+r'\.'+task+'\.(['+environment+']+)+\.(['+location+']+)\Z', flags=re.I)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.