Extension talk:RegexParserFunctions

From MediaWiki.org
Jump to: navigation, search

Could you put more samples of use here, Please. For example, what are the different using between match and replacement. --Roc michael 12:17, 15 June 2007 (UTC)

Hi Roc! More detailed examples and usage instructions are availble on the RegexParserFunctions Project HomePage. --Jimbojw 04:42, 16 June 2007 (UTC)
Thank for your help, Jimbojw.--Roc michael 07:10, 16 June 2007 (UTC)


I really love this extension!

I also have an improvement idea:

replacing like {{#regex:RegexParserFunctions|%^.*/(.*)$%|$1}} would be much more usefull if it would be possible to do something with the replace string $1. For example {{#regex:RegexParserFunctions|%^.*/(.*)$%| {{#len:$1}}}}. Certainly $1 as variable for such operations wouldn't be a good variable name because it also could be possible that in the match string is a $1. In this case we would get a problem. Perhaps you could make this work as variable for wikis with variable extension like: {{#regex:RegexParserFunctions|%^.*/(.*)$%| {{#len:{{#var:$1}}}}}} This would offer much new possiblities like run another regex over the matches. --Danwe 20:25, 22 February 2009 (UTC)

Contents

[edit] Helpful examples

I think it could be helpful if everybody who wrote a regex which is generally helpful adds it here. I Wrote one which dissolves all links inside a given string:

{{#regex: {{{1|}}} |%\[\[:?(?(?=[^\[\]\{{!}}]*\{{!}})[^\[\]\{{!}}]*\{{!}}{{!}})([^\[\]]*)\]\]%|$1}}

Example: "text text [[:link|linktext]] and [[link]]" is going to be "text text linktext and link" --Danwe 22:15, 22 February 2009 (UTC)

[edit] Remove Category links

%\[\[Category?(?(?=[^\[\]\{{!}}]*\{{!}})[^\[\]\{{!}}]*\{{!}}{{!}})([^\[\]]*)\]\]\n%

this removes cat links like [[Category:Test]]. But it doesn't work if the category name includes : like in [[Category:Test: Abc]]. How would I do that? Big thanks! --Subfader 20:32, 5 July 2009 (UTC)

/\[\[Category:.*\]\]\s*/s --Subfader 00:09, 8 July 2009 (UTC)

[edit] Doesn't work in 1.16alpha

Maybe you can have a look now already before everyone using this extension will come here ;) --Subfader 00:09, 8 July 2009 (UTC)

Using Extension:RegexFunctions instead :) --Subfader 22:45, 11 July 2009 (UTC)

[edit] New function for array regex (regexall)

I created a function regexall to get all matches of a regex. It returns all matches separated so you could use the result with the array extension for example. Replacing or getting some parts from brackets only is not possible yet. Please feel free to implement new features as well or to improve my code.

The function is based on the original regexParserFunction so I post it here for you as well:

/**
* Performs regular expression search and returns ALL matches separated.
* @param Parser $parser Instance of running Parser.
* @param String $subject Input string to evaluate.
* @param String $pattern Regular expression pattern - must use /, | or % delimiter
* @param String $separator String to separate all the matches.
* @param Int    $offset First match to print out. Negative values possible: -1 means last match.
* @param Int    $length Maximum matches for print out.
* @return String Result of all matching text parts separated by a string.
*/
function regexallParserFunction( &$parser , $subject=null , $pattern=null , $seperator=', ' , $offset=0 , $length=null ) {
        if ($subject===null || $pattern===null) return '';
        $acceptable = '/^([\\/\\|%]).*\\1[imsu]*$/';
 
        if (!preg_match($acceptable, $pattern)) return wfMsg('regexp-unacceptable',$pattern);
 
        if (preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER)) {
 
                if (is_numeric($offset)) {
                        if (!empty($length) &&  is_numeric($length)){
                                $matches = array_slice($matches, $offset, $length);
                        } else {
                                $matches = array_slice($matches, $offset);     
                        }
                } else {return '';}
 
                $output = ''; //$end = ($end or ($end >= count($matches)) ? $end : count($matches) );
 
                for($count = 0; $count < count($matches); $count++)
                {
                        $output.= ( $output != '' ? $seperator : '' );
                        $output.= trim( $matches[$count][0] );
                }
                return $output;                              
        }         
        return '';
}

--Danwe 14:02, 1 December 2009 (UTC)

[edit] New, (mostly) compatible regex extension with advanced features

I have just released my extension Regex Fun which also provides #regex function as provided by this function but with some extended features like:

  • More sophisticated regular expression validation. Invalid expression will now output a formated inline error, catchable by #iferror. This might be the only reason why my new extension could break some of you old code, if you rely on invalid regex returning empty string. But you shouldn't have invalid regex anyway.
  • 'r' flag for #regex returning "" in case no replacement was done.
  • 'e' flag allows to parse replacement string after reference insertion before input text replacement.
  • Three more useful parser functions dealing with regular expressions: #regexall, #regex_var and #regexquote

So there is no real reason to stick to the use of RegexParserFunctions right now. So far I have used that extension as well and time after time I have created my own functions, keeping it compatible. --Danwe 07:32, 5 November 2011 (UTC)

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox