The module "mreg_replace" of the Mastrave modelling library

 

Daniele de Rigo

 


Copyright and license notice of the function mreg_replace

 

 

Copyright © 2006,2007,2008,2009,2010 Daniele de Rigo

The file mreg_replace.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.

Function declaration

 

 

[answer] = mreg_replace( string , pattern , replacement , options )

Description

 

 

Perform a perl-like extended regular expression search and replace on a given string .

Input arguments

 

 


 string             ::cellstring::
                    string or cell-array of strings on which to apply the
                    regular expression search and replace 

 pattern            ::string::
                    perl-like extendend regular expression search
                    Valid operators are:

                        operator               meaning
                    ──────────────────────────────────────────────────────
                         expr | expr  | boolean or: both  expr s can match
                                       (one at a time)
                    ──────────────────────────────────────────────────────
                        ( expr )       subexpression: group the content
                                        expr  as if it were a single
                                       token;  expr  is marked and can be
                                       referred to in the subsequent part
                                       of  pattern  or returned in
                                        replacement  using the operator
                                       \ n  (see it) for the  n th marked
                                       subexpression
                    ──────────────────────────────────────────────────────
                        \ n            match the  n th marked 
                                       subexpression
                    ──────────────────────────────────────────────────────
                        (?: expr )     non-marked subexpression: it
                                       cannot be referred to nor returned
                                       but still consumes the characters 
                                       it matches, as in the basic
                                       ( expr ) operator
                    ──────────────────────────────────────────────────────
                        (?= expr )     forward asserion: non-marked
                                       subexpression which is a test on
                                       the characters following the
                                       current matching point performed
                                       without actually consuming any
                                       character
                    ──────────────────────────────────────────────────────
                        (?<= expr )    backward asserion: non-marked
                                       subexpression which is a test on
                                       the characters preceding the
                                       current matching point performed
                                       without actually consuming any
                                       character
                    ──────────────────────────────────────────────────────
                        (?! expr )     forward negative asserion: test on
                                       the characters following the 
                                       matching point to ensure they do
                                       not match  expr 
                    ──────────────────────────────────────────────────────
                        (?<! expr )    backward negative asserion: test on
                                       the characters preceding the 
                                       matching point to ensure they do
                                       not match  expr 
                    ──────────────────────────────────────────────────────
                        .              any single character except newline
                    ──────────────────────────────────────────────────────
                        [ expr ]       any single character contained
                                       in  expr ; if  expr  contains a
                                       range of characters in the form
                                        c1 - c2  (e.g a-z or 0-9), then
                                       all characters from  c1  to  c2 
                                       will match
                    ──────────────────────────────────────────────────────
                        [^ expr ]      any single character not included
                                       in the set  expr  
                    ──────────────────────────────────────────────────────
                        \w             [a-zA-Z0-9_] words
                    ──────────────────────────────────────────────────────
                        \W             [^\w] non-words
                    ──────────────────────────────────────────────────────
                        \d             [0-9] digits
                    ──────────────────────────────────────────────────────
                        \D             [~\d] non-digits
                    ──────────────────────────────────────────────────────
                        \s             [ \t\r\n\v\f] spaces
                    ──────────────────────────────────────────────────────
                        \S             [^\s] non-spaces
                    ──────────────────────────────────────────────────────
                        \n             newline character
                    ──────────────────────────────────────────────────────
                        \t             tab character
                    ──────────────────────────────────────────────────────
                        tok { n , m }  match the token  tok  if it's
                                       repeated from a minimum of  n 
                                       times to a maximum of  m  times
                                       (greedy quantifier: it matches as
                                       much as possible ── up to the
                                       maximum number of permitted times) 
                    ──────────────────────────────────────────────────────
                        tok { n }       tok { n , n } (greedy quantifier) 
                    ──────────────────────────────────────────────────────
                        tok { n ,}      tok { n ,65535}
                                       (greedy quantifier)
                    ──────────────────────────────────────────────────────
                        tok *           tok {0,}  (greedy quantifier) 
                    ──────────────────────────────────────────────────────
                        tok ?           tok {0,1} (greedy quantifier)
                    ──────────────────────────────────────────────────────
                        tok +           tok {1,}  (greedy quantifier)
                    ──────────────────────────────────────────────────────
                        tok { n , m }?| lazy quantifier: it matches the 
                                       minimum number of permitted times 
                    ──────────────────────────────────────────────────────
                        tok { n ,}?     tok { n ,65535} (lazy quantifier)
                    ──────────────────────────────────────────────────────
                        tok *?          tok {0,}  (lazy quantifier) 
                    ──────────────────────────────────────────────────────
                        tok ??          tok {0,1} (lazy quantifier)
                    ──────────────────────────────────────────────────────
                        tok +?          tok {1,}  (lazy quantifier)
                    ──────────────────────────────────────────────────────
                       ^               begin of the line
                    ──────────────────────────────────────────────────────
                       $               end of the line (newline excluded) 



 replacement        ::string::
                    replacement string
                    Valid operators are:

                        operator               meaning
                    ──────────────────────────────────────────────────────
                        \ n            return the  n th subexpression
                                       marked in  pattern 
                    ──────────────────────────────────────────────────────
                        \n             newline character
                    ──────────────────────────────────────────────────────
                        \t             tab character



 options            ::string::
                    modifier codes (default: 'g')
                    Valid modifiers are:

                        operator               meaning
                    ──────────────────────────────────────────────────────
                        i              non case-sensitive matching
                    ──────────────────────────────────────────────────────
                        s              if passed, add newline in the
                                       . operator set of characters
                    ──────────────────────────────────────────────────────
                        g              "global" : replace all matches,
                                       and not just the first one. 



Examples of usage

   mreg_replace(                                                    ...
      '/  1 /* 2 **/ 3 /**4*/ 5 ' , '/\*.*?\*/' , '[]', 'g'         ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'g'   ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'i'   ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'gi'  ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4}?)' , '[\1]' , 'gi' ...
   )
   mreg_replace(                                                    ...
      { 'Foo bar baz' , 'foO BaR  bAz' } ,                          ...
      '([a-z ]{2,4}?)' , '[\1]' , 'gi'                              ...
   )



Shell requirements: cat, perl



version: 0.2.15

Example of usage

 

 


 string             ::cellstring::
                    string or cell-array of strings on which to apply the
                    regular expression search and replace 

 pattern            ::string::
                    perl-like extendend regular expression search
                    Valid operators are:

                        operator      |         meaning
                    ------------------+------------------------------------
                         expr | expr  | boolean or: both  expr s can match
                                      | (one at a time)
                    ------------------+------------------------------------
                        ( expr )      | subexpression: group the content
                                      |  expr  as if it were a single
                                      | token;  expr  is marked and can be
                                      | referred to in the subsequent part
                                      | of  pattern  or returned in
                                      |  replacement  using the operator
                                      | \ n  (see it) for the  n th marked
                                      | subexpression
                    ------------------+------------------------------------
                        \ n           | match the  n th marked 
                                      | subexpression
                    ------------------+------------------------------------
                        (?: expr )    | non-marked subexpression: it
                                      | cannot be referred to nor returned
                                      | but still consumes the characters 
                                      | it matches, as in the basic
                                      | ( expr ) operator
                    ------------------+------------------------------------
                        (?= expr )    | forward asserion: non-marked
                                      | subexpression which is a test on
                                      | the characters following the
                                      | current matching point performed
                                      | without actually consuming any
                                      | character
                    ------------------+------------------------------------
                        (?<= expr )   | backward asserion: non-marked
                                      | subexpression which is a test on
                                      | the characters preceding the
                                      | current matching point performed
                                      | without actually consuming any
                                      | character
                    ------------------+------------------------------------
                        (?! expr )    | forward negative asserion: test on
                                      | the characters following the 
                                      | matching point to ensure they do
                                      | not match  expr 
                    ------------------+------------------------------------
                        (?<! expr )   | backward negative asserion: test on
                                      | the characters preceding the 
                                      | matching point to ensure they do
                                      | not match  expr 
                    ------------------+------------------------------------
                        .             | any single character except newline
                    ------------------+------------------------------------
                        [ expr ]      | any single character contained
                                      | in  expr ; if  expr  contains a
                                      | range of characters in the form
                                      |  c1 - c2  (e.g a-z or 0-9), then
                                      | all characters from  c1  to  c2 
                                      | will match
                    ------------------+------------------------------------
                        [^ expr ]     | any single character not included
                                      | in the set  expr  
                    ------------------+------------------------------------
                        \w            | [a-zA-Z0-9_] words
                    ------------------+------------------------------------
                        \W            | [^\w] non-words
                    ------------------+------------------------------------
                        \d            | [0-9] digits
                    ------------------+------------------------------------
                        \D            | [~\d] non-digits
                    ------------------+------------------------------------
                        \s            | [ \t\r\n\v\f] spaces
                    ------------------+------------------------------------
                        \S            | [^\s] non-spaces
                    ------------------+------------------------------------
                        \n            | newline character
                    ------------------+------------------------------------
                        \t            | tab character
                    ------------------+------------------------------------
                        tok { n , m } | match the token  tok  if it's
                                      | repeated from a minimum of  n 
                                      | times to a maximum of  m  times
                                      | (greedy quantifier: it matches as
                                      | much as possible -- up to the
                                      | maximum number of permitted times) 
                    ------------------+------------------------------------
                        tok { n }     |  tok { n , n } (greedy quantifier) 
                    ------------------+------------------------------------
                        tok { n ,}    |  tok { n ,65535}
                                      | (greedy quantifier)
                    ------------------+------------------------------------
                        tok *         |  tok {0,}  (greedy quantifier) 
                    ------------------+------------------------------------
                        tok ?         |  tok {0,1} (greedy quantifier)
                    ------------------+------------------------------------
                        tok +         |  tok {1,}  (greedy quantifier)
                    ------------------+------------------------------------
                        tok { n , m }?| lazy quantifier: it matches the 
                                      | minimum number of permitted times 
                    ------------------+------------------------------------
                        tok { n ,}?   |  tok { n ,65535} (lazy quantifier)
                    ------------------+------------------------------------
                        tok *?        |  tok {0,}  (lazy quantifier) 
                    ------------------+------------------------------------
                        tok ??        |  tok {0,1} (lazy quantifier)
                    ------------------+------------------------------------
                        tok +?        |  tok {1,}  (lazy quantifier)
                    ------------------+------------------------------------
                       ^              | begin of the line
                    ------------------+------------------------------------
                       $              | end of the line (newline excluded) 



 replacement        ::string::
                    replacement string
                    Valid operators are:

                        operator      |         meaning
                    ------------------+------------------------------------
                        \ n           | return the  n th subexpression
                                      | marked in  pattern 
                    ------------------+------------------------------------
                        \n            | newline character
                    ------------------+------------------------------------
                        \t            | tab character



 options            ::string::
                    modifier codes (default: 'g')
                    Valid modifiers are:

                        operator      |         meaning
                    ------------------+------------------------------------
                        i             | non case-sensitive matching
                    ------------------+------------------------------------
                        s             | if passed, add newline in the
                                      | . operator set of characters
                    ------------------+------------------------------------
                        g             | "global" : replace all matches,
                                      | and not just the first one. 



Examples of usage

   mreg_replace(                                                    ...
      '/  1 /* 2 **/ 3 /**4*/ 5 ' , '/\*.*?\*/' , '[]', 'g'         ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'g'   ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'i'   ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4})' , '[\1]' , 'gi'  ...
   )
   mreg_replace(                                                    ...
      'Foo bar baz foO BaR  bAz' , '([a-z ]{2,4}?)' , '[\1]' , 'gi' ...
   )
   mreg_replace(                                                    ...
      { 'Foo bar baz' , 'foO BaR  bAz' } ,                          ...
      '([a-z ]{2,4}?)' , '[\1]' , 'gi'                              ...
   )



Shell requirements: cat, perl


version: 0.2.15

Support

 

 

The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science.  You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module.  Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable.  However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based.  Reporting a problem that you found using Mastrave may help the developer team to find a possible bug.  Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems.  Thank you for your collaboration.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo

This page is licensed under a Creative Commons Attribution-NoDerivs 3.0 Italy License.

This document is also part of the book:
de Rigo, D. (2012). Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling. http://mastrave.org/doc/MTV-1.012-1


Valid XHTML 1.0 Transitional