Search code examples
pythonregexnon-greedy

Problem using greedy and non-greedy regular expressions


I can't understand what I am doing wrong. I only need to extract router_name and port number. the router name is "C1900_ROUTER1_SR7" and port is "4/1/4" and it could exist "B:C1900_ROUTER1_SR7......" instead of "A:C1900_ROUTER1_SR7.........."

string = "A:C1900_ROUTER1_SR7# 

A:C1900_ROUTER1_SR7# echo "<team:script>"

<team:script>

A:C1900_ROUTER1_SR7# /environment no more 

A:C1900_ROUTER1_SR7# 

A:C1900_ROUTER1_SR7# show port 4/1/4 

"

regex_S2 = '(A|B):(?P<routername>.*?)#\s*show port\s*(?P<port>.*?)\s*\n'

match_L0_iter = re.findall(regex_S2, string, flags=re.DOTALL)

The routername captured group results = "C1900_ROUTER1_SR7# A:C1900_ROUTER1_SR7# echo "<pnm:script>" <pnm:script> A:C1900_ROUTER1_SR7# /environment no more A:C1900_ROUTER1_SR7# A:C1900_ROUTER1_SR7"


Solution

  • You could update your pattern to match non whitespace chars at the end instead of a non greedy .*? To match the router name, you might also use a negated character class [^#]+ matching any char except a #

    To match either A or B you could use a character class.

    [AB]:(?P<routername>[^#]+)#\s*show port\s*(?P<port>\S+)
    

    Regex demo

    If you must match the rest of the line as well including a newline, you can append \s*\n as in your pattern.