Ramy
Ramy

Reputation: 21271

rubular result differs from IRB

I jumped through a lot of hoops to get this string:

"<html>\n<head>\n<script language=\"JavaScript\">  \n\n        ////////////////////////////////////////////////////////////////  \n        // This [base64 encoder and decoder] was written by Tyler Akins and has been placed in the  \n        // public domain.  It would be nice if you left this header intact.  \n        // Base64 code from Tyler Akins -- http://rumkin.com  \n        var keyStr = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";  \n\n        function encode64(input) {  \n           var output = \"\";  \n           var chr1, chr2, chr3;  \n           var enc1, enc2, enc3, enc4;  \n           var i = 0;  \n\n           do {  \n              chr1 = input.charCodeAt(i++);  \n              chr2 = input.charCodeAt(i++);  \n              chr3 = input.charCodeAt(i++);  \n\n              enc1 = chr1 >> 2;  \n              enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  \n              enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  \n              enc4 = chr3 & 63;  \n\n              if (isNaN(chr2)) {  \n                 enc3 = enc4 = 64;  \n              } else if (isNaN(chr3)) {  \n                 enc4 = 64;  \n              }  \n\n              output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +  \n                 keyStr.charAt(enc3) + keyStr.charAt(enc4);  \n           } while (i < input.length);  \n\n           return output;  \n        }  \n        function decode64(input) {  \n           var output = \"\";  \n           var chr1, chr2, chr3;  \n           var enc1, enc2, enc3, enc4;  \n           var i = 0;  \n\n           // remove all characters that are not A-Z, a-z, 0-9, +, /, or =  \n           input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");  \n\n           do {  \n              enc1 = keyStr.indexOf(input.charAt(i++));  \n              enc2 = keyStr.indexOf(input.charAt(i++));  \n              enc3 = keyStr.indexOf(input.charAt(i++));  \n              enc4 = keyStr.indexOf(input.charAt(i++));  \n\n              chr1 = (enc1 << 2) | (enc2 >> 4);  \n              chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  \n              chr3 = ((enc3 & 3) << 6) | enc4;  \n\n              output = output + String.fromCharCode(chr1);  \n\n              if (enc3 != 64) {  \n                 output = output + String.fromCharCode(chr2);  \n              }  \n              if (enc4 != 64) {  \n                 output = output + String.fromCharCode(chr3);  \n              }  \n           } while (i < input.length);  \n\n           return output;  \n        }  \n\n        // end of Tyler Akins' code  \n        ////////////////////////////////////////////////////////////////  \n  function escapePluses(s) {  \n       return s.replace(/\\+/g, \"%2B\");  \n  }  \n  function getFragment(thisuri) {  \n      var pound = thisuri.indexOf(\"#\");  \n      if (pound == -1) {  \n          return null;  \n      } else {  \n          return thisuri.substr(pound + 1);  \n      }  \n  }  \n  function saveFragment() {  \n      var fragment = getFragment(document.URL);  \n      if (fragment != null) {  \n          var pre_marker  = \"&aka_frag=\";  \n          var g_req = decode64(document.relay.pubcookie_g_req.value);  \n          var header_end = g_req.indexOf(pre_marker) + pre_marker.length;  \n          var req_head = g_req.substr(0,header_end);  \n          var req_foot = g_req.substr(header_end);  \n         if ((req_foot.length > 0) && (req_foot.charAt(0) != '&')) {  \n              req_foot = req_foot.substr(req_foot.indexOf(\"&\"));  \n          }  \n         var new_req = req_head + escapePluses(encode64(fragment)) + req_foot;  \n          document.relay.pubcookie_g_req.value = encode64(new_req);  \n      }  \n  }  \n\n  function doStuff() {  \n      saveFragment();  \n      document.relay.submit();  \n  }  \n\n//  setTimeout('doStuff()', 1000);  \n</script></head>\n<body onLoad=\"doStuff()\">\n<form method=post action=\"https://weblogin.server.com/\" name=relay>\n<input type=hidden name=pubcookie_g_req value=\"b25lPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdHdvPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdGhyZWU9MSZmb3VyPWE1YSZmaXZlPUdFVCZzaXg9c2lhbS1wcm8ucWEuYWthbWFpLmNvbSZzZXZlbj1MMk52Ym1acFozQmhjbk5sWDNCdmNuUnomZWlnaHQ9JmFrYV9mcmFnPSZob3N0bmFtZT1zaWFtLXByby5xYS5ha2FtYWkuY29tJm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPS0xNTE4MTQyNjAwJmZsYWc9MA==\">\n<input type=hidden name=post_stuff value=\"\">\n<input type=hidden name=relay_url value=\"https://siam-pro.qa.server.com/PubCookie.reply\">\n<noscript>\n<p align=center>You do not have Javascript turned on,   please click the button to continue.\n<p align=center>\n<input type=submit name=go value=Continue>\n</noscript>\n</form>\n</html>\n"

i then want to match that string on this regex:

<form [^>]*action=(?:\\*"([^"]*)\\*"|([^" >]*))[^>]* name=relay>(.*?)<\/form>

This works as expected in rubular, but in IRB (1.9.3) i get the following:

1.9.3p448 :147 > data =~/<form [^>]*action=(?:\\*"([^"]*)\\*"|([^" >]*))[^>]* name=relay>(.*?)<\/form>/
=> nil 

What am I doing wrong here?

Upvotes: 1

Views: 130

Answers (2)

the Tin Man
the Tin Man

Reputation: 160581

Regex and HTML/XML are not good bedfellows. WHEN the HTML changes the odds are really good your pattern will break. A parser reduces the chance of your code breaking significantly. For instance, it's easy to anticipate that parameters in a tag can change their order:

<form method="post" action="https://weblogin.server.com/" name="relay">

The form could change to one of these:

<form method="post" action="https://weblogin.server.com/" name="relay" >...</form>
<form method="post" action="https://weblogin.server.com/" name="relay1" >...</form>
<form name="relay" method="post" action="https://weblogin.server.com/">...</form>
<form name="relay" method="post" action="https://weblogin.server.com/">...</form >

If any of those happen the regex is instantly broken.

A parser wouldn't care about those changes.

require 'nokogiri'

html = "<html>\n<head>\n<script language=\"JavaScript\">  \n\n        ////////////////////////////////////////////////////////////////  \n        // This [base64 encoder and decoder] was written by Tyler Akins and has been placed in the  \n        // public domain.  It would be nice if you left this header intact.  \n        // Base64 code from Tyler Akins -- http://rumkin.com  \n        var keyStr = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";  \n\n        function encode64(input) {  \n           var output = \"\";  \n           var chr1, chr2, chr3;  \n           var enc1, enc2, enc3, enc4;  \n           var i = 0;  \n\n           do {  \n              chr1 = input.charCodeAt(i++);  \n              chr2 = input.charCodeAt(i++);  \n              chr3 = input.charCodeAt(i++);  \n\n              enc1 = chr1 >> 2;  \n              enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  \n              enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  \n              enc4 = chr3 & 63;  \n\n              if (isNaN(chr2)) {  \n                 enc3 = enc4 = 64;  \n              } else if (isNaN(chr3)) {  \n                 enc4 = 64;  \n              }  \n\n              output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +  \n                 keyStr.charAt(enc3) + keyStr.charAt(enc4);  \n           } while (i < input.length);  \n\n           return output;  \n        }  \n        function decode64(input) {  \n           var output = \"\";  \n           var chr1, chr2, chr3;  \n           var enc1, enc2, enc3, enc4;  \n           var i = 0;  \n\n           // remove all characters that are not A-Z, a-z, 0-9, +, /, or =  \n           input = input.replace(/[^A-Za-z0-9\\+\\/\\=]/g, \"\");  \n\n           do {  \n              enc1 = keyStr.indexOf(input.charAt(i++));  \n              enc2 = keyStr.indexOf(input.charAt(i++));  \n              enc3 = keyStr.indexOf(input.charAt(i++));  \n              enc4 = keyStr.indexOf(input.charAt(i++));  \n\n              chr1 = (enc1 << 2) | (enc2 >> 4);  \n              chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  \n              chr3 = ((enc3 & 3) << 6) | enc4;  \n\n              output = output + String.fromCharCode(chr1);  \n\n              if (enc3 != 64) {  \n                 output = output + String.fromCharCode(chr2);  \n              }  \n              if (enc4 != 64) {  \n                 output = output + String.fromCharCode(chr3);  \n              }  \n           } while (i < input.length);  \n\n           return output;  \n        }  \n\n        // end of Tyler Akins' code  \n        ////////////////////////////////////////////////////////////////  \n  function escapePluses(s) {  \n       return s.replace(/\\+/g, \"%2B\");  \n  }  \n  function getFragment(thisuri) {  \n      var pound = thisuri.indexOf(\"#\");  \n      if (pound == -1) {  \n          return null;  \n      } else {  \n          return thisuri.substr(pound + 1);  \n      }  \n  }  \n  function saveFragment() {  \n      var fragment = getFragment(document.URL);  \n      if (fragment != null) {  \n          var pre_marker  = \"&aka_frag=\";  \n          var g_req = decode64(document.relay.pubcookie_g_req.value);  \n          var header_end = g_req.indexOf(pre_marker) + pre_marker.length;  \n          var req_head = g_req.substr(0,header_end);  \n          var req_foot = g_req.substr(header_end);  \n         if ((req_foot.length > 0) && (req_foot.charAt(0) != '&')) {  \n              req_foot = req_foot.substr(req_foot.indexOf(\"&\"));  \n          }  \n         var new_req = req_head + escapePluses(encode64(fragment)) + req_foot;  \n          document.relay.pubcookie_g_req.value = encode64(new_req);  \n      }  \n  }  \n\n  function doStuff() {  \n      saveFragment();  \n      document.relay.submit();  \n  }  \n\n//  setTimeout('doStuff()', 1000);  \n</script></head>\n<body onLoad=\"doStuff()\">\n<form method=post action=\"https://weblogin.server.com/\" name=relay>\n<input type=hidden name=pubcookie_g_req value=\"b25lPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdHdvPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdGhyZWU9MSZmb3VyPWE1YSZmaXZlPUdFVCZzaXg9c2lhbS1wcm8ucWEuYWthbWFpLmNvbSZzZXZlbj1MMk52Ym1acFozQmhjbk5sWDNCdmNuUnomZWlnaHQ9JmFrYV9mcmFnPSZob3N0bmFtZT1zaWFtLXByby5xYS5ha2FtYWkuY29tJm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPS0xNTE4MTQyNjAwJmZsYWc9MA==\">\n<input type=hidden name=post_stuff value=\"\">\n<input type=hidden name=relay_url value=\"https://siam-pro.qa.server.com/PubCookie.reply\">\n<noscript>\n<p align=center>You do not have Javascript turned on,   please click the button to continue.\n<p align=center>\n<input type=submit name=go value=Continue>\n</noscript>\n</form>\n</html>\n"

doc = Nokogiri::HTML(html)

form = doc.at('form')
puts form.to_html
# >> <form method="post" action="https://weblogin.server.com/" name="relay">
# >> <input type="hidden" name="pubcookie_g_req" value="b25lPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdHdvPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdGhyZWU9MSZmb3VyPWE1YSZmaXZlPUdFVCZzaXg9c2lhbS1wcm8ucWEuYWthbWFpLmNvbSZzZXZlbj1MMk52Ym1acFozQmhjbk5sWDNCdmNuUnomZWlnaHQ9JmFrYV9mcmFnPSZob3N0bmFtZT1zaWFtLXByby5xYS5ha2FtYWkuY29tJm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPS0xNTE4MTQyNjAwJmZsYWc9MA=="><input type="hidden" name="post_stuff" value=""><input type="hidden" name="relay_url" value="https://siam-pro.qa.server.com/PubCookie.reply"><noscript>
# >> <p align="center">You do not have Javascript turned on,   please click the button to continue.
# >> </p>
# >> <p align="center">
# >> <input type="submit" name="go" value="Continue"></p>
# >> </noscript>
# >> </form>

form['action'] # => "https://weblogin.server.com/"
input = form.at('input')
input['name'] # => "pubcookie_g_req"
input['value'] # => "b25lPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdHdvPXNpYW0tcHJvLnFhLmFrYW1haS5jb20mdGhyZWU9MSZmb3VyPWE1YSZmaXZlPUdFVCZzaXg9c2lhbS1wcm8ucWEuYWthbWFpLmNvbSZzZXZlbj1MMk52Ym1acFozQmhjbk5sWDNCdmNuUnomZWlnaHQ9JmFrYV9mcmFnPSZob3N0bmFtZT1zaWFtLXByby5xYS5ha2FtYWkuY29tJm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPS0xNTE4MTQyNjAwJmZsYWc9MA=="

Nokogiri is the favored XML/HTML parser for Ruby. It's fast, easy to use, and has been very robust in my use.

Upvotes: 3

struthersneil
struthersneil

Reputation: 2750

You need to use a multiline regex here--enable multiline matching using the m modifier, e.g.

/<form [^>]*action=(?:\\*"([^"]*)\\*"|([^" >]*))[^>]* name=relay>(.*?)<\/form>/m

Upvotes: 0

Related Questions