Powerbuilder and regular expression

Regex will help you in text processing,


In computing, a regular expression, also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. Read more

Okay, and in the real world ?

The following powerscript demonstrate how to use regex to parse text and lets code more readable than if we write ourself text extraction code using pos function call.

constant string CRLF = "~r~n"
constant string TAB = "~t"
long i, ll_match_count, ll_entry, ll_entry_count
string ls_lib_list, ls_entries, ls_txt
uo_regex lrx_libs, lrx_entries
lrx_libs = create uo_regex
lrx_entries = create uo_regex
//  GetLibraryList ( )
//    Returns the current library list with complete paths. Multiple libraries are separated by commas.
lrx_libs.initialize( "[^,]+" /*as_pattern*/, true/*ab_globalscope*/, true/*ab_casesensitive */)
// LibraryDirectoryEx returns a tab-separated list with one object per line. 
//    The format of the list is:
//      name ~t date/time modified ~t comments ~t   type~n
lrx_entries.initialize( "([^\t]+)\t([^\t]+)\t([^\t]*)\t([^[\r\n]+)", true, false)
ls_lib_list = GetLibraryList()
ll_match_count = lrx_libs.search( ls_lib_list )
ls_txt = "GetLibraryList() + LibraryDirectoryEx( ..., DirAll! )" + CRLF
for i = 1 to ll_match_count
  ls_txt += TAB + string(i)+") "+lrx_libs.Match( i ) + CRLF
  ls_entries = LibraryDirectoryEx( lrx_libs.Match( i ) , DirAll! )
  // Group 1 : name
  // Group 2 : DateTime modified
  // Group 3 : Comments (if any)
  // Group 4 : type
  ls_txt += lrx_entries.replace( ls_entries, "~t~t* \1 (\4) modified on \3 // \2~r~n")
mle_1.text = ls_txt
destroy lrx_libs
destroy lrx_entries

The result looks like that :

uo_regex demo

This sample use the uo_regex component (PBNI) that is freely available at http://sebastien.kirche.free.fr/powerbuilder/en.html

More about regex

  • Cheat sheet :