Extension talk:RegexFunctions

From MediaWiki.org
Jump to navigation Jump to search

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)

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)

Bug: $wgRegexFunctionsDisable not working with 'rsplit' and 'rreplace'[edit]

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[edit]

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)


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)


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)


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)

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)

Same stack error[edit]

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)

Get domain from URL[edit]

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)

Escape pipe[edit]

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

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

Parameter 4 to rreplace[edit]

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)

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)
I can confirm the same problem and fix in 1.18.0. --Subfader 20:05, 6 December 2011 (UTC)
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)
Officially patched in version 1.4.2. --Skizzerz 16:18, 21 March 2012 (UTC)

Alternative in regex[edit]

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)
Thank you, Skizzerz! //oss

Creating a smart embed template[edit]

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 |?[edit]

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)

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)

New PHP versions: The /e modifier is deprecated[edit]

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)

Deprecated: preg_replace(): /e[edit]

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)

I've got the same error. Has anyone been able to find a solution for this? --Glameglumps33 (talk) 02:42, 18 February 2015 (UTC)
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)