Discussions » Development

Regular Expressions Hotline (general requests for regex help)

§
Posted: 2015-08-04

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.

§
Posted: 2015-08-04
Edited: 2015-08-07

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
§
Posted: 2015-08-04
Edited: 2015-08-04
  • 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.

§
Posted: 2015-08-07

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.

§
Posted: 2015-08-07

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.

§
Posted: 2015-08-07

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.

§
Posted: 2015-08-07
Edited: 2015-08-08

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

§
Posted: 2015-08-08
Edited: 2015-08-08

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'))
});
§
Posted: 2015-08-08

thank you vzjrz!!!

§
Posted: 2015-08-13
Edited: 2015-08-13

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

Post reply

Sign in to post a reply.