Share this page 

Use Regular Expression from PowerscriptTag(s): WinApi/Registry


Support for regular expression in Powerscript is minimal, you need to use an external COM object like VBScript.RegExp to do something useful.

This example takes a string, remove spaces and replaces them with a single ";"

OLEObject re
int li_retcode
string s
string value

re = Create OLEObject
li_retcode = re.ConnectToNewObject("VBScript.RegExp")
re.Pattern = "\s+"
re.Global = True
s = "4  2"
value = re.Replace("4   2" , ";")
MessageBox("", value) // 4;2
re.DisconnectObject()

RegExp can be used to parse a string using complex expression. This example extracts each word in a given string.

OLEObject re
OLEObject matches
int li_retcode, li_count, i
String ls_String

re = Create OLEObject
li_retcode = re.ConnectToNewObject("VBScript.RegExp")
re.Pattern = "(\w+)"  // match words
re.Global = True
matches = re.Execute("This a Powerbuilder HowTo")
li_count = matches.Count

FOR i = 0 TO li_count - 1
    MessageBox(String(i) , String(matches.item[i].Value))
NEXT

re.DisconnectObject()
if you change the pattern to
re.Pattern = "\w*[o]\w*"  // match words with the letter "o"
the matches will be only on "Powerbuilder" and "HowTo".

Designing the regexp expression can be complex, an online tester is useful to get it right :
https://regex101.com/https://regex101.com/

More infos about RegExp in VBscript :
https://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm
https://developer.rhino3d.com/guides/rhinoscript/vbscript-regexp-objects/