TCL - Regexp - Regular Expression

Card Puncher Data Processing

About

The regular expression in TCL:

  • regexp - Match a regular expression against a string
  • regsub - Perform substitutions based on regular expression pattern matching

Regexp

Syntax

The regexp command has as syntax:

regexp ?switches? exp string ?matchVar? ?subMatchVar1? ?subMatchVar2? ...?

The character ? say that the parameter is optional. where :

  • switches : I don't know
  • exp is the regular expression
  • string is the string to test against the regular expression
  • matchVar contain the string which match the regular expression exp
  • subMatchVar1…n string that were matched by parenthesized bits in exp

Snippet

Example made with OWB - OMB (Oracle Meta Base) Language

OMB+> regexp {PREFIX_(.*)_POSTFIX} "PREFIX_TO_FIND_POSTFIX" matched sub1 sub2 sub3
1
# return 1 to indicate that a match was found
OMB+> puts $matched
PREFIX_TO_FIND_POSTFIX
# Return the matched string
OMB+> puts $sub1
TO_FIND
# return the string that match for the parenthese

Regsub

Perform substitutions based on regular expression pattern matching.

Syntax

  regsub ?switches? exp string subSpec ?varName?

This command matches the regular expression exp against string, replace the match with subSpec and either copies string to the variable whose name is given by varName or returns string if varName is not present.

Switches: If the initial arguments to regsub start with - then they are treated as switches. :

  • -all:All ranges in string that match exp are found and substitution is performed for each of these ranges. Without this switch only the first matching range is found and substituted.
  • Others: here

Snippet

How to suppress a string ?

OMB+> set a "string_with_part_to_suppress"
string_with_part_to_suppress
OMB+> regsub {with_part_to_suppress} $a "" a
1
OMB+> puts $a
string_

How to suppress a curly brace?

Below a method to suppress the curly brace that are use by tcl to escape special characters. When you use the OMB language of OWB, you can get as return value this double curly brace “{}” that indicates that you have no values.

proc P_SuppressCurly Vs_StringWithCurly {
	# Suppress all curly
	regsub -all "({|})" $Vs_StringWithCurly "" Vs_StringWithoutCurly
	return $Vs_StringWithoutCurly
}

How to replace a string ?

OMB+> set Vs_OriginalString "Start_ToReplace_End"
Start_ToReplace_End
OMB+> set Vs_StringToReplace "ToReplace"
ToReplace
OMB+> set Vs_StringToPut "IsReplaced"
IsReplaced
OMB+> regsub $Vs_StringToReplace $Vs_OriginalString $Vs_StringToPut Vs_OutputString
1
OMB+> puts $Vs_OutputString
Start_IsReplaced_End

Documentation Reference





Discover More
Regexp
Multilingual Regular Expression Syntax (Pattern)

Regular expression are Expression that defines a pattern in text. This is therefore a language that permits to define structure of a text. They are a mathematically-defined concept, invented by Stephen...
Card Puncher Data Processing
Tool Command Language (TCL)

Tcl (from “Tool Command Language”) is a scripting language created by John Ousterhout. intended to be embedded into applications ( Oracle Warehouse Builder use it as scripting language : Mixed with...



Share this page:
Follow us:
Task Runner