Discussions » Développement

Regular Expressions Hotline (general requests for regex help)

§
Posté le: 04/08/2015

Regular Expressions Hotline (general requests for regex help)

userscripts(-mirror).org had/has a few topics on getting help with regex strings; I couldn't find any here. (If there's any existing discussion, please let me know.)

Hope this will be helpful for general users (e.g. myself) and developers.

§
Posté le: 04/08/2015
Édité le: 07/08/2015

I'll start it off...

I'm trying to get scripts to trigger (or not) on links that are most-likely blogs or forums.

I'ld like the logic to be: "a forward-slash, followed by any of the keywords, followed by any other text".

e.g. this should catch and

I've tried several attempts, none seem to be working on this example link - http://www.hydrogenaud.io/forums/lofiversion/index.php/t48386.html

/^/(blog(s)|lofiversion|archive(s)|board(s)|group(s)|discussion(s)|forum(s)|thread(s)|show(post|thread|topic)|view(thread|topic)|(index\.php))/

/^https?:\/\/[^\/]*(blog(s)|lofiversion|archive(s)|board(s)|group(s)|discussion(s)|forum(s)|thread(s)|show(post|thread|topic)|view(thread|topic)|(index\.php))/

*/(blog(s)|lofiversion|archive(s)|board(s)|group(s)|discussion(s)|forum(s)|thread(s)|show(post|thread|topic)|view(thread|topic)|(index\.php))*


I'm using Greasemonkey 3.3 - Firefox 38.1.0esr

Can anyone help?

woxxomMod
§
Posté le: 04/08/2015
Édité le: 04/08/2015
  • As you can see in your 2nd regexp the forward slash / in the 1st one should be escaped as \/ otherwise it denotes an end of the expression: /^\/(blog....
  • (s) this does nothing useful, so if you want to make the s optional use ?: blogs?

Use https://regex101.com to instantly see what your expression does.

§
Posté le: 07/08/2015

Thanx alot for the help, that did the trick.

I changed the string to - this:

/^\/(blogs?|lofiversion|archives?|boards?|groups?|discussions?|forums?|threads?|show(post|thread|topic)|view(thread|topic)|(index\.php))/

which does satisfy that parser.

Trouble is-- it still doesn't work.

I tried reducing the string to:

/^\/(blogs?|lofiversion|archives?|boards?|groups?|discussions?|forums?|threads?)/

thinking the nested pipes might be hosing up things. But it still doesn't work.

But even worse than all of this, I discovered thru some testing--with a clean profile, with no other add-on than GM 3.3--that not only do these strings not work, but Global Excludes aren't triggering.

It's weird-- I tested this: /^https?:\/\/[^\/]*\.amazon\.com\// and also http://www.amazon.com/* and neither works.

So now, I'll have to go to Greasemonkey github and see if anyone has reported this; if not, I'll have to post a report of both my problems.

§
Posté le: 07/08/2015

First if you're going to search the entire url then remove the ^ since that tries to match your regex at the very start of the url:

/\/(blogs?|lofiversion|archives?|boards?|groups?|discussions?|forums?|threads?|show(post|thread|topic)|view(thread|topic)|(index\.php))/

Like wOxxOm said regex101 is a great way to see what is actually going on: https://regex101.com/r/pM9pT5/1

I tried the global excludes and they work for me on GM 3.3. Something must be wrong on your end.

§
Posté le: 07/08/2015

I tried the global excludes and they work for me on GM 3.3. Something must be wrong on your end.

Yes; Obviously.

First if you're going to search the entire url then remove the ^ since that tries to match your regex at the very start of the url:

/\/(blogs?|lofiversion|archives?|boards?|groups?|discussions?|forums?|threads?|show(post|thread|topic)|view(thread|topic)|(index\.php))/

Thanks a bunch, that worked spot-on.

§
Posté le: 07/08/2015
Édité le: 08/08/2015

i have this script

$('a[href^="browse.php?imdb"]:lt(5)').each( function() {
var $this = $(this);
var href = $this.attr('href').replace(/(browse\.php((\?|&)imdb\=)[0-9]*)/g,'/details\.php((\?|&)id\=)[0-9]*)\&toseeders\=1/');
$this.attr('href', href );
});


i want to replace for example first link "browse.php?imdb=988824" with "details.php?id=1475609&toseeders=1" from here

thank you

§
Posté le: 08/08/2015
Édité le: 08/08/2015

This should do it:

$('.tableTorrents a[href^="browse.php?imdb="]').each(function () {
    $(this).attr('href', $(this).closest('tr').find('a[href^="details.php?id="]').attr('href').replace(/details\.php\?id=(\d+).*/, 'details.php?id=$1&toseeders=1'))
});
§
Posté le: 08/08/2015

thank you vzjrz!!!

§
Posté le: 13/08/2015
Édité le: 13/08/2015

Thanks for Regular Expression 100
An other online tool to learn regex which work great too:
RegExr v2.0: Learn, Build, & Test RegEx - by gskinner.com

About Regexp , You can drop an eye here:
Regexp questions collected in the forum (from my Bookmarks) [forum.userstyles.org

Poster une réponse

Connectez-vous pour poster une réponse.