How to use a variable inside a regular expression?

后端 未结 10 1870
青春惊慌失措
青春惊慌失措 2020-11-22 03:29

I\'d like to use a variable inside a regex, how can I do this in Python?

TEXTO = sys.argv[1]

if re.search(r\"\\b(?=\\         


        
相关标签:
10条回答
  • 2020-11-22 03:37

    I agree with all the above unless:

    sys.argv[1] was something like Chicken\d{2}-\d{2}An\s*important\s*anchor

    sys.argv[1] = "Chicken\d{2}-\d{2}An\s*important\s*anchor"
    

    you would not want to use re.escape, because in that case you would like it to behave like a regex

    TEXTO = sys.argv[1]
    
    if re.search(r"\b(?<=\w)" + TEXTO + "\b(?!\w)", subject, re.IGNORECASE):
        # Successful match
    else:
        # Match attempt failed
    
    0 讨论(0)
  • 2020-11-22 03:41

    I needed to search for usernames that are similar to each other, and what Ned Batchelder said was incredibly helpful. However, I found I had cleaner output when I used re.compile to create my re search term:

    pattern = re.compile(r"("+username+".*):(.*?):(.*?):(.*?):(.*)"
    matches = re.findall(pattern, lines)
    

    Output can be printed using the following:

    print(matches[1]) # prints one whole matching line (in this case, the first line)
    print(matches[1][3]) # prints the fourth character group (established with the parentheses in the regex statement) of the first line.
    
    0 讨论(0)
  • 2020-11-22 03:41

    you can try another usage using format grammer suger:

    re_genre = r'{}'.format(your_variable)
    regex_pattern = re.compile(re_genre)  
    
    0 讨论(0)
  • 2020-11-22 03:46

    I find it very convenient to build a regular expression pattern by stringing together multiple smaller patterns.

    import re
    
    string = "begin:id1:tag:middl:id2:tag:id3:end"
    re_str1 = r'(?<=(\S{5})):'
    re_str2 = r'(id\d+):(?=tag:)'
    re_pattern = re.compile(re_str1 + re_str2)
    match = re_pattern.findall(string)
    print(match)
    

    Output:

    [('begin', 'id1'), ('middl', 'id2')]
    
    0 讨论(0)
  • 2020-11-22 03:47

    You can use format keyword as well for this.Format method will replace {} placeholder to the variable which you passed to the format method as an argument.

    if re.search(r"\b(?=\w)**{}**\b(?!\w)".**format(TEXTO)**, subject, re.IGNORECASE):
        # Successful match**strong text**
    else:
        # Match attempt failed
    
    0 讨论(0)
  • 2020-11-22 03:49
    rx = r'\b(?<=\w){0}\b(?!\w)'.format(TEXTO)
    
    0 讨论(0)
提交回复
热议问题