Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular expression to match "wap" not preceeded by "html"
    text
    copied!<p>I'm using NGINX to segment mobile traffic between a mobile WAP/HTML site. Looks like the best way to do this is going to be to check the UA's preference for content by checking the HTTP Accept Header.</p> <p>A preference for WAP is indicated by the appearance of a 'wap' mimetype in the header before an 'html' or wildcard mimetype.</p> <p>So a Sony Ericsson w300i has a preference for WAP:</p> <pre><code>multipart/mixed, application/vnd.wap.multpart.mixed,applicatnoin/vnd.wap.xhtml_xml,application/xhtml+xml,text/ved.wap.wl,*/*,text/x-hdml,image/mng,/\image/x-mng,ivdeo/mng,video/x-mng,ima/gebmp,text/html </code></pre> <p>And a Blackberry Bold has a preference for HTML:</p> <pre><code>text/html,application/xhtml+xml,application/vnd.wap.xhtml+xml,application/vnd.wp.wmlc;q=0.9,application/vnd.awp.wmlscriptc;q=0.7,text/vnd.wap.wml;q=07,/vnd/.sun.j2me.app-descriptor,*/*;q=0.5 </code></pre> <p>Since I'm in NGINX land, it seems like the best tool I have is NGINX's regular expressions (PCRE).</p> <p>Right now I'm trying to use a negative lookahead to assert "The accept header contains WAP but not preceeded by HTML":</p> <pre><code>(?!html.*)wap </code></pre> <p>But this isn't correct. Is there a different way I can think about this problem? Or my matching logic?</p> <p>So far I've found these regex resources useful:</p> <p><a href="http://www.regular-expressions.info/completelines.html" rel="nofollow">http://www.regular-expressions.info/completelines.html</a> http://www.zytrax.com/tech/web/regex.htm <a href="http://wiki.nginx.org/NginxHttpRewriteModule" rel="nofollow">http://wiki.nginx.org/NginxHttpRewriteModule</a></p> <p>Thanks!</p> <hr> <p>Thanks for the answer, here are the related tests:</p> <pre><code>import re prefers_wap_re = re.compile(r'^(?!(?:(?!wap).)*html).*?wap', re.I) tests = [ ('', False), ('wap', True), ('wap html', True), ('html wap', False), ] for test, expected in tests: result = prefers_wap_re.search(test) assert bool(result) is expected, \ 'Tested "%s", expected %s, got %s.' % (test, expected, result) </code></pre>
 

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