Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're correctly specify the grouping you want for your regular expression, but you're not using it when printing it out.</p> <p>Your regular expression is:</p> <pre><code>mac = re.match /Device ID: ([\S]+)/ </code></pre> <p>and this matches the whole line <code>Device ID: SEP1C17D3415659</code>, putting the part you want (<code>SEP1C17D3415659</code>) into a group so you can get at it later. However you then print it out using</p> <pre><code>o.puts mac </code></pre> <p>This gives the whole match, not just the group. What you want is this instead:</p> <pre><code>o.puts mac[1] </code></pre> <p>which specifies just the group.</p> <p>Also, as Cody says, <code>puts</code> adds a newline on each call. If you want both matches to print on the same line try something like:</p> <pre><code>o.print "#{mac[1]} #{ip[1]}\n" </code></pre> <p>instead.</p> <hr> <p><strong>Edit:</strong></p> <p>I'm not familiar with <code>net/telnet</code> or the command you're running, but it looks like you're running a certain command, then filtering for certain lines and then printing some info from those lines. If this is the case, then any lines that don't match will give you <code>nil</code> for <code>mac</code> and <code>ip</code> so you'll get "undefind method []" on those lines.</p> <p>If this is the case you can simply use:</p> <pre><code>o.print "#{mac[1]} #{ip[1]}\n" unless mac.nil? </code></pre> <p>You might find it worth while to restructure your code slightly to better express what you're doing, by combining the two regular expressions into one. Without knowing the structure of the actual lines I can't create the real expression you'll need, but it would look something like this:</p> <pre><code>run.each_line do |line| match = line.match /^Device ID: ([\S]+), IP address: ([\S]+)$/ #these are the lines we want next unless match #skip lines we don't want o.print "#{match[1]} #{match[2]}\n" #print the details we're interested in end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload