Extension talk:RegexFunctions/Archive 1

Latest comment: 4 years ago by Skizzerz in topic PCR compatibility?

If you have errors about Magic Words not being found when installing RegexFunctions, you have downloaded the wrong version of the extension. Please see the installation instructions for more details.

Ryan, This looks extremely useful but I can't figure out how to implement it. Could you post an example? Like maybe...

{{#rmatch: $/(#/)|C|{{Copyrighted Material}}}} 

Thanks, CWinDC 06:35, 9 January 2009 (UTC)Reply

Well, if you viewed the syntax notes, you'd notice that the string is passed into the function, so {{#rmatch:This is the string I wish to match|(.*) match|\1 return}} would yield "This is the string I wish to return." All parameters such as {{{1}}} are valid inside of these functions as well. --Skizzerz 21:42, 9 January 2009 (UTC)Reply

Bug: $wgRegexFunctionsDisable not working with 'rsplit' and 'rreplace'

Version 1.3 (SVN Rev 48342) RegexFunctions.php:
method rmatch() will check whether it is disabled in $wgRegexFunctionsDisable by doing if(in_array( 'rmatch' , $wgRegexFunctionsDisable)) return;.
rsplit() and rreplace() also do this, except that they check for 'rmatch', too, instead of checking for 'rsplit' or 'rreplace' as expected.
Thanks. This is a useful extension. - Sheepy, 09:53, 12 March 2009 (UTC)

Stack Trace Error When Installed

The following is displayed on the mediawiki main page when the plug-in is enabled in LocalSettings.php. Any ideas on how to solve this? I am running MediaWiki versions 1.15.1 and up to date MySQL and PHP. There should be no other conflicts involving other plug-ins, as I was modeling my wiki page off of another with the same set of plug-ins. Thanks!

Internal error

Magic word 'rmatch' not found

Backtrace:

  1. 0 /var/lib/mediawiki/includes/MagicWord.php(244): Language->getMagic(Object(MagicWord))
  2. 1 /var/lib/mediawiki/includes/MagicWord.php(197): MagicWord->load('rmatch')
  3. 2 /var/lib/mediawiki/includes/parser/Parser.php(4034): MagicWord::get('rmatch')
  4. 3 /var/lib/mediawiki/extensions/RegexFunctions/RegexFunctions.php(49): Parser->setFunctionHook('rmatch', Array)
  5. 4 [internal function]: wfRegexFunctions()
  6. 5 /var/lib/mediawiki/includes/Setup.php(310): call_user_func('wfRegexFunction...')
  7. 6 /var/lib/mediawiki/includes/WebStart.php(129): require_once('/var/lib/mediaw...')
  8. 7 /var/lib/mediawiki/index.php(42): require_once('/var/lib/mediaw...')
  9. 8 {main}


Make sure that you have all of the necessary files (RegexFunctions.php, RegexFunctions.i18n.php, and RegexFunctions.magic.php). If you don't have all three of them, re-download the extension (get the "Development Version (trunk)" version from the extension distributor) --Skizzerz 04:43, 18 February 2010 (UTC)Reply


Thank you for helping. I did all have three files, but tried a fresh install anyway getting the "Development Version" trunk, but it still is giving me the same error when I try to enable it in the LocalSettings.php. Everything else seems to be working just fine within the Wiki (general functions and all other installed plug-ins). Any ideas? Thanks again.

I'm not entirely sure how to help you since I cannot reproduce this issue -- the magic words are loading just fine for me. Perhaps some more information on your setup would help me figure out what is messing up for you:
  • PHP version
  • MediaWiki language
  • Server version (e.g. Apache 2.2 or whatever)
  • Have you edited any core files (e.g. anything that isn't called "LocalSettings.php")? If so, does reverting the changes you made to them fix the issue?
Please respond with the answers to these --Skizzerz 22:43, 18 February 2010 (UTC)Reply


Thanks once again for helping. I am running the following versions of everything: MediaWiki v1.15.1, PHP v5.2.6-3ubuntu4.1 (apache2handler), and MySQL v5.0.75-0ubuntu10.2. I am running the following plug-ins: in Special Pages there is Replace Text and Semantic Forms, in Parser Hooks there is ArrayExtensions, LoopFunctions, Loops, ParserFunctions, SemanticMediaWiki, SMWHaloExtension, StringFunctions, Variables, and VideoFlash, and in Other there is GoogleCharts, Semantic Gardening, Semantic History, Semantic Result Formats, and StubManager. I have not edited any core files, simply LocalSettings.php and only to add all of these plug-ins.

OK, check through your PHP and apache's error log for something similar to "'rmatch' is not a valid magic thingy". Let me know if you find it or not. --Skizzerz 06:30, 19 February 2010 (UTC)Reply


There nothing in either error log concerning "rmatch" or "magic". I apologize that this issue is so hard to diagnose/fix. There isn't anything at all popping up either in the logs at the times that I turn on the extension and try to access the page, getting the error. Please let me know if you have any other ideas on what it could possibly be or if there is any more information I can give to help in the diagnosis. Thanks!

I unfortunately have no idea how to fix your issue then, sorry. Perhaps it'll magically work once 1.16.0 gets released, but that's a while off still. --Skizzerz 08:00, 1 March 2010 (UTC)Reply

i fix this as follow (i know it's not the best way, but it works for me)

$wgHooks['LanguageGetMagic'][] = 'wfRegexFunctionsLanguageGetMagic';
#$wgExtensionMessagesFiles['RegexFunctionsMagic'] = $dir . 'RegexFunctions.i18n.magic.php';
function wfRegexFunctions() {
	global $wgParser, $wgExtRegexFunctions;

	$wgExtRegexFunctions = new ExtRegexFunctions();
	$wgParser->setFunctionHook( 'regexmatch', array(&$wgExtRegexFunctions, 'runRegexmatch') );
	$wgParser->setFunctionHook( 'regexsplit', array(&$wgExtRegexFunctions, 'runRegexsplit') );
	$wgParser->setFunctionHook( 'regexreplace', array(&$wgExtRegexFunctions, 'runRegexreplace') );
}

function wfRegexFunctionsLanguageGetMagic( &$magicWords, $langCode = "en" ) {
	switch ( $langCode ) {
		default:
		$magicWords['regexmatch']          = array ( 0, 'regexmatch' );
		$magicWords['regexsplit']          = array ( 0, 'regexsplit' );
		$magicWords['regexreplace']         = array ( 0, 'regexreplace' );
	}
	return true;
}

additional i replace rmatch with regexmatch etc and the functions with runRegexmatch like this:

	function runRegexsplit ( &$parser, $string = , &$pattern = , $piece = 0 ) {
		global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable;
		if(in_array('regexsplit', $wgRegexFunctionsDisable))

See box on top of page for information about this --Skizzerz 16:16, 20 May 2010 (UTC)Reply

Same stack error

I get the exact same stack error with 1.15. I have checked the downloaded files; they're ok. I have placed the include at the top of local settings. No help. I'd prefer not to patch the code.

See box on top of page for information about this --Skizzerz 16:16, 20 May 2010 (UTC)Reply

Get domain from URL

The lack of examples makes it hard to get it working :(

I want to grab the domains of different kind of URLs, but without subdomains. E.g.

{{#rmatch:http://maps.google.com/maps?hl=|(\.)?[a-zA-Z0-9]*\.com|$#}}
{{#rmatch:http://google.com/maps?hl=|(\.)?[a-zA-Z0-9]*\.com|$#}}
{{#rmatch:http://www.google.com|(\.)?[a-zA-Z0-9]*\.com|$#}}

should all return google.com. But I can't seem to return any results. MW 1.16a, the extension works fine tho.

Any ideas? Cheers --Subfader 11:42, 15 August 2010 (UTC)Reply

Escape pipe

--Svdubl 14:31, 2 September 2010 (UTC) I need to use pipe character ("|") in regexp. how can I escape it?Reply

Use a template that produces the pipe character, such as {{! }}. --Skizzerz 05:06, 8 September 2010 (UTC)Reply

Parameter 4 to rreplace

MediaWiki  1.16.1
PHP        5.3.5-pl0-gentoo (cgi-fcgi)
PostgreSQL 8.4.5
RegexFunctions 1.4.1 (repo rev 80761)

My replacements weren't working and I was seeing this:

PHP Warning:  Parameter 4 to ExtRegexFunctions::rreplace()
expected to be a reference, value given in
/var/www/localhost/htdocs/mediawiki/includes/parser/Parser.php on line 2920

I saw what Skizzers did with parameter 3 ($pattern) in the repo, so I did the same with 4 in my downloaded copy:

 # function rreplace ( &$parser, $string = '', $pattern = '', &$replace = '' )
   function rreplace ( &$parser, $string = '', $pattern = '',  $replace = '' )

Not sure if that's proper, but it seems to be working. ~Michael Allantalk 04:07, 25 January 2011 (UTC)Reply

I also confirm that, it seems like a bug causing #rreplace not to work at all (MW-1.16.2, RegexFunctions-1.4.1). And the fix suggested by Michael Allan indeed works. Ankostis 09:19, 22 May 2011 (UTC)Reply
I can confirm the same problem and fix in 1.18.0. --Subfader 20:05, 6 December 2011 (UTC)Reply
Fix also works for 1.19. This is a general warning with PHP 5.3 installations. --Morbus Iff (talk) 14:10, 18 February 2012 (UTC)Reply
Officially patched in version 1.4.2. --Skizzerz 16:18, 21 March 2012 (UTC)Reply

Alternative in regex

How can one make an alternative in regex since the | symbol is used as a delimeter for parameters?

{{#rmatch:MYTEXT|(cat|dog)|FOUND|NOT FOUND|0}} using syntax as above returns error, because of mistreating of the | between cat and dog.

Make a template {{! }} exactly as it is on this wiki or wikipedia, then use that. --Skizzerz 22:56, 8 June 2012 (UTC)Reply
Thank you, Skizzerz! //oss

Creating a smart embed template

I was trying to create an embed template that can create widgets but can't get it to work. Maybe one of you guys can help me out :) As the extensions RegexFunctions and Widgets are not installed here I cannot show the results in "real time" though and maybe the problem lies with the extension Widgets.

This outputs the wikitext I want it to:

{{#rmatch:http://www.youtube.com/watch?v=MOiyD26cJ2A|https?:\/\/www.youtube.com\/.*v=(.*)|{{widget:YouTube{{!}}id=$1}}}}

It creates the wikitext looking like this: {{widget:YouTube|id=MOiyD26cJ2A}}

However, widgets need a hash before they work and this does not work:

{{#rmatch:http://www.youtube.com/watch?v=MOiyD26cJ2A|https?:\/\/www.youtube.com\/.*v=(.*)|{{widget:#YouTube{{!}}id=$1}}}}

The hash makes it show up like this:

Fout in id=MOiyD26cJ2A|widget YouTube|id=MOiyD26cJ2A

(Fout is Dutch for Error)

If I get this to work I'd like to create a template that can be used like this: {{embed:http://www.youtube.com/watch?v=MOiyD26cJ2A}}. The template will also support other embed widgets like SlideShare, Scribd etc.

Not possible to use |?

Do I get it right, that its not possible to write a regex like

{{#regex:Foo|%(foo|bar)%|baz}}

because it would include a |? --212.184.135.196 14:14, 22 June 2014 (UTC)Reply

This is the same as every other parser function and/or template, you cannot include a | in the parameter. You can however, use another template to insert the | character. A typical template for that purpose is {{! }}. --Skizzerz 18:44, 22 June 2014 (UTC)Reply

New PHP versions: The /e modifier is deprecated

Error in PHP 5.5.13:

preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

--Subfader (talk) 17:11, 2 October 2014 (UTC)Reply

Deprecated: preg_replace(): /e

PHP 5.5.13 here:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

--Subfader (talk) 19:40, 14 October 2014 (UTC)Reply

I've got the same error. Has anyone been able to find a solution for this? --Glameglumps33 (talk) 02:42, 18 February 2015 (UTC)Reply
A fix was implemented in the git repository on the REL1_28 and master branches by Alexia (diff). The REL1_28 and master snapshots should include it. -71.36.99.196 23:34, 29 December 2016 (UTC)Reply

PCR compatibility?

I'm trying to make /^(?U)(?:\d+:)?(\D+)(?::.*)?$/ match "parameter" in \1 on input string "1:parameter:*" This works when tested in https://www.debuggex.com/ in PCRE mode but not in #rmatch.

I assume all the functions do is pass the regex on to PHP, right?

But assuming that, I cannot make internal option setting work, for example (?U). Setting the modifier after the delimiter works, however. But neither character classes in the notation [:alpha:], nor non-capturing groups (?:...) work.

If I try /^(\d+:)?(\D+)(:.*)?$/U then I get "param" in \2, however.

Am I missing something?

I understand there have been changes with PHP 7, namely with regard to compatibility with POSIX, but the above examples are all in the manual

Also what's the deal with delimiters? Are they required? I'm running RegexFunctions REL1_31-8ba02d5 on MW 1.31.5 on PHP 7.0.33-0ubuntu0.16.04.12 (cli)

--Ahmad Gharbeia أحمد غربية (talk) 14:07, 18 March 2020 (UTC)Reply

There appear to be some bugs with inline options. The regex is not passed as-is to the PHP functions, some cleanup is performed on it before being passed to PHP. This process apparently prevents inline options from working correctly. I can work on a fix, but any new version would not support 1.31 so you would need to upgrade your wiki to obtain the fix. --Skizzerz 15:38, 18 March 2020 (UTC)Reply
Thank you so much for your prompt reply. I look forward to the coming version of RegexFunctions. It's a great extension that helps me solve several problems. I will also likely upgrade to the next LTS version of MW when it's out. Meanwhile, I'm looking into alternative solutions in formulating my RegEx expressions. --Ahmad Gharbeia أحمد غربية (talk) 21:25, 18 March 2020 (UTC)Reply
Seems that nothing changed much between 1.31 and recent versions of mediawiki, so my fix should work fine on the LTS version (despite what I said above). This brought a lot of underlying changes to the extension, but it's mostly backwards compatible. Delimiters are not required, but must be forward slashes if you decide to specify them (no other delimiters are supported). I'm working on updating the extension page but you already grab the new version (download latest master). --Skizzerz 23:08, 18 March 2020 (UTC)Reply
Return to "RegexFunctions/Archive 1" page.