MiniM. Saving time.

| About | Download | Tools | Knowledge Base | How to Buy |

MiniM Overview

Getting Started




Cache Tools

GT.M Tools

Knowledge Base




MiniM Knowledge Base | Jul 26, 2011

Useful Regular Expressions

Regular expressions are much power and significant and may be counted as one more programming language. Regular expressions may be much useful in application servers which check user unput, inputs data from external data sources and in many user-frienfly tasks.

MUMPS language does not contain builtin regular expressions. Nearest to regular expressiona are MUMPS patterns. To extent possibilities of application servers based on the MiniM Database Server there was implemented some system functions:

  • $ZPCREM[ATCH] for matching string to regular expression
  • $ZPCRES[EARCH] for searching occurences in the string by regular expression
  • $ZPCRER[EPLACE] for replacing substring defined by regular expression

Prefix PCRE means Perl Compatible Regular Expressions. This functions are built on the pcre library. Here are some of the benefits of regular expressions.

In many cases, programmers are also treated to a special libraries of regular expressions. In most cases they all may be used in MiniM with regards to syntax and $ZPCREXXX functions arguments meaning.

Remove leading whitespaces

USER>s str="  abc  def  "

USER>s regexp="^\s+"

USER>w """",$zpcrer(str,regexp,""),""""
"abc  def  "

Note that the \s symbol means not only whitespace, it also includes tabstop, new line, form feed and carriage return symbols.

Remove trailing whitespaces

USER>s str="  abc  def  "

USER>s regexp="\s+$"

USER>w """",$zpcrer(str,regexp,""),""""
"  abc  def"

Remove leading and trailing whitespaces

USER>s str="  abc  def  "

USER>s regexp="^\s+|\s+$"

USER>w """",$zpcrer(str,regexp,"","g"),""""
"abc  def"

Remove repeatable whitespaces between words except leading and trailing ones

USER>s str="  abc  def  "

USER>s regexp="(\w)( )+(\w)"

USER>w """",$zpcrer(str,regexp,"$1 $3","gp"),""""
"  abc def  "

Replace repeatable whitespaces to single and remove leading and trailing whitespaces

USER>s str="  abc  def  "

USER>s regexp="^\s+|(?<=\s)( )+|\s+$"

USER>w """",$zpcrer(str,regexp,"","g"),""""
"abc def"

Remove all non-alpha symbols from begin of the string

USER>s str="  abc  def  "

USER>s regexp="^([[:^alpha:]])+"

USER>w """",$zpcrer(str,regexp,""),""""
"abc  def  "

Get list of all number sequences

USER>s str=" 123 789.as456ee333"

USER>s regexp="\d+"

USER>s found=$zpcres(str,regexp,"g")

USER>f i=1:1:$ll(found) w $lg(found,i),!

Simple IPv4 address matching

USER>s regexp="^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"

USER>w $zpcrem("",regexp),!

USER>w $zpcrem("222.333.444.555",regexp),!

Full IPv4 address matching

USER>s r="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$"

USER>w $zpcrem("",r),!

USER>w $zpcrem("222.333.444.555",r),!

Eugene Karataev

Copyright (C) Eugene Karataev
Info Support