Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

Regex (python): how to match for certain query

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, i.e.:

11.alfa.cad.base

i don't want to get lines with "11" nor "d.b", 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!

question from:https://stackoverflow.com/questions/65617566/regex-python-how-to-match-for-certain-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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])+.
  • Here, the . matches any character instead of a dot only.
  • If you don't want to match 11, you should not use a quantifier for either the group or the character class
  • Repeating the capture group ()+ would capture the value of the last iteration, you want the group value as a whole so you can repeat the character class instead
  • As strings like 1 and db are hardcoded, you don't really have to capture them

Taking 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

1.db.([a-z]+)+.([a-z]+)
  ^    ^          ^
  Dot  group 1    group 2

Regex demo

q = re.compile(ID+r'.'+task+'.(['+environment+']+)+.(['+location+']+)', flags=re.I)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...