Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20578 > unrolled thread

Regexp confusion

Started byJJ <duh@nah.meh>
First post2013-07-08 00:26 +0700
Last post2013-07-09 03:58 +0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Regexp confusion JJ <duh@nah.meh> - 2013-07-08 00:26 +0700
    Re: Regexp confusion dann90038@gmail.com - 2013-07-07 12:21 -0700
      Re: Regexp confusion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-07 22:18 +0200
    Re: Regexp confusion Denis McMahon <denismfmcmahon@gmail.com> - 2013-07-07 20:28 +0000
    Re: Regexp confusion JJ <duh@nah.meh> - 2013-07-08 08:24 +0700
      Re: Regexp confusion Andreas Bergmaier <andber93@web.de> - 2013-07-08 17:51 +0200
        Re: Regexp confusion JJ <duh@nah.meh> - 2013-07-09 03:58 +0700

#20578 — Regexp confusion

FromJJ <duh@nah.meh>
Date2013-07-08 00:26 +0700
SubjectRegexp confusion
Message-ID<sh5vv3omcuc2$.1ls4uvq7bzu5d$.dlg@40tude.net>
I'm trying to filter out URLs that match below conditions using regexp.

* Contains ".feedburner.com" or ".feedportal.com". e.g.:
  http://feeds.feedburner.com/any
  http://feedportal.com/any

* Starts with "feed" and ends with ".gif".
  http://feed.thesite.com/any/blah.gif

* Starts with "feed" and the following characters contains ".gif?".
  http://feedsite.com/any/blah.gif?var=blah

I'm not an expert at regular expression and I'm using below regexp, but the
first URL fails to match.

var badurls=/\.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)/;

var url="http://feeds.feedburner.com/any";
var result=badurls.test(url);
console.log(result); //false

var url="http://feed.thesite.com/any/blah.gif";
var result=badurls.test(url);
console.log(result); //true

var url="http://feedsite.com/any/blah.gif?var=blah";
var result=badurls.test(url);
console.log(result); //true

//However, if I swapped the regexp like below, it works. Why?

var badurls=/feed.*\.gif($|\?)|\.feed(burner|sportal)\.com\//;

var url="http://feeds.feedburner.com/any";
var result=badurls.test(url);
console.log(result); //true

var url="http://feed.thesite.com/any/blah.gif";
var result=badurls.test(url);
console.log(result); //true

var url="http://feedsite.com/any/blah.gif?var=blah";
var result=badurls.test(url);
console.log(result); //true

[toc] | [next] | [standalone]


#20580

Fromdann90038@gmail.com
Date2013-07-07 12:21 -0700
Message-ID<c1df98f5-f298-4131-b7a9-fd6c054b92bf@googlegroups.com>
In reply to#20578
To JJ

it works for me in js if I use
\.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)*

and I get matches on all 3 with the string
http://feeds.feedburner.com/any
http://feed.thesite.com/any/blah.gif
http://feedsite.com/any/blah.gif?var=blah

it also works with 
\.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)
if my string alone is
http://feed.thesite.com/any/blah.gif

so, I'd think there's something after the ".gif" which isn't $ end of input or "?" character

[toc] | [prev] | [next] | [standalone]


#20581

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-07-07 22:18 +0200
Message-ID<9161127.hJ6kbhveRM@PointedEars.de>
In reply to#20580
dann90038@gmail.com wrote:

> To JJ

<http://www.netmeister.org/news/learn2quote.html>
<http://jibbering.com/faq/#posting>
<http://PointedEars.de/faq#posting>

> it works for me in js if I use
> \.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)*

Which is nonsense.  Neither can the end of input/line be repeated, nor is 
the “?” allowed to be repeated (unescaped) in a “http(s):”-URI-Reference.
 
> and I get matches on all 3 with the string
> http://feeds.feedburner.com/any
> http://feed.thesite.com/any/blah.gif
> http://feedsite.com/any/blah.gif?var=blah

And you also get a match for

  http://feedsite.com/any/blah.gif??????????var=blah

which certainly is _not_ a valid “http(s):”-URI-Reference.
 
> it also works with
> \.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)
> if my string alone is
> http://feed.thesite.com/any/blah.gif
> 
> so, I'd think there's something after the ".gif" which isn't $ end of
> input or "?" character

RFC 3986, Appendix B, contains a *proper* regular expression for matching 
URIs that can be adapted to result in

  /\.feed(burner|sportal)\.com\/|feed[^?]*\.gif(\?[^#]*)?(#.*)?$/

or (non-greedy matching, probably not worth the incompatiblity here)

  /\.feed(burner|sportal)\.com\/|feed[^?]*\.gif(\?.*?)?(#.+)?$/

It is possible that this is still not what the OP needs as then there can 
only be query-part and fragment-part if a resource name was specified (“/a|
b(c?)/” ≡ “/(?:a)|(?:b(c?))/”).  A more viable solution might be

  /(\.feed(burner|sportal)\.com\/|feed[^?]*\.gif)(\?[^#]*)?(#.*)?$/
   ^                                            ^
or

  /(\.feed(burner|sportal)\.com\/|feed[^?]*\.gif)(\?.*?)?(#.+)?$/
   ^                                            ^
Note the extra parentheses that group the outer alternation.  Also, where 
non-greedy matching is supported, potentially more efficient non-capturing 
parentheses are supported, for example:

  /(?:\.feed(?:burner|sportal)\.com\/|feed[^?]*\.gif)(\?.*?)?(#.+)?$/

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#20582

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-07-07 20:28 +0000
Message-ID<krcj27$p4d$1@dont-email.me>
In reply to#20578
On Mon, 08 Jul 2013 00:26:11 +0700, JJ wrote:

> I'm trying to filter out URLs that match below conditions using regexp.

The text you've shown and the words you've used do not agree completely, 
but I've tried to resolve them as best I can.

> * Contains ".feedburner.com" or ".feedportal.com". e.g.:
>   http://feeds.feedburner.com/any http://feedportal.com/any

^.*feed(burner|portal)\.com.*$

> * Starts with "feed" and ends with ".gif".
>   http://feed.thesite.com/any/blah.gif

^(http:\/\/)?feed.*\.gif$

> * Starts with "feed" and the following characters contains ".gif?".
>   http://feedsite.com/any/blah.gif?var=blah

^(http:\/\/)?feed.*\.gif.*$

My first attempt at combining these would be:

/^(http:\/\/)?.*feed((burner|portal)\.com|.*\.gif).*$/

Tested here: http://www.sined.co.uk/tmp/regexjj.htm

There may be some potential there for false positives, which might be 
improved by checking for a word boundary before the f of feed, and after 
\.com and \.gif

However, I suspect that even so there may be potential false positives, 
for example any gif image on feedtheworld.com.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#20585

FromJJ <duh@nah.meh>
Date2013-07-08 08:24 +0700
Message-ID<up86xx5xsxv8.12bgmg63bgj2$.dlg@40tude.net>
In reply to#20578
Thank you everyone, and sorry for the trouble.

I mainly use classic Opera (non-Chromium), and just found out that it fails
only in Opera. A bug I assume.

The initial, non swapped regexp works fine in IE, FF, and Chromium.

[toc] | [prev] | [next] | [standalone]


#20588

FromAndreas Bergmaier <andber93@web.de>
Date2013-07-08 17:51 +0200
Message-ID<kren6r$2vf$1@news.albasani.net>
In reply to#20585
JJ schrieb:
> Thank you everyone, and sorry for the trouble.
>
> I mainly use classic Opera (non-Chromium), and just found out that it fails
> only in Opera. A bug I assume.

The test case you provided works well in Opera version 12.15 - which one 
did you use? Or is it something that leads to different input to your 
function in Opera?

  Bergi

[toc] | [prev] | [next] | [standalone]


#20589

FromJJ <duh@nah.meh>
Date2013-07-09 03:58 +0700
Message-ID<to5fc0lc2gdr$.l1eutpp08fc3$.dlg@40tude.net>
In reply to#20588
On Mon, 08 Jul 2013 17:51:57 +0200, Andreas Bergmaier wrote:
> The test case you provided works well in Opera version 12.15 - which one 
> did you use? Or is it something that leads to different input to your 
> function in Opera?

The first test that use:
var badurls=/\.feed(burner|sportal)\.com\/|feed.*\.gif($|\?)/;

On Opera v11.64 and v12.15 (both 32-bit for Windows), the results are:
false, true, true.

But on Opera-Chromium, Chromium, IE8, FF18 (all 32-bit for Windows), the
results are true, true, true.

Actual inputs are the image URLs from news feed entries viewed using Opera's
Feed Preview feature (i.e.: not viewed via Opera mail/feed client). My
Opera's Feed Preview feature has been heavily customized for my own needs. I
don't use Opera's content blocking, since it's unreliable (sometimes won't
match).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web