KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Obtaining code spacing with Regular expressions
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: November 24, 2020

With project mode being available in newer versions of 4D, it will not be unusual to parse the code as text.

With the 4D code editor, 4D will apply automatic transformations to make the code look "prettier" and easier to read.

One of the ussual features is the automated tabbing. If code is attempted to be modified or added and it needs to consider the tab spacing of the lines around it this can be determined with a regex.

Each line starts with a "\r" then each tab is a "\t".
To find the spacing of a specific line of code, perform a regex search for "\r(\t)*" with the Match Regex command. This will match the start of a new line, and then the asterisk will tell the search that \t can occur 0 or more times. This will find the longest occurance of this string of the start of a new line and tabs.

$found_b:=Match regex("\r(\t)*"; $code_t; $startPos_l; $foundPos_l; $lengthFound_l)

The Match regex command will return the position and length of the first found item from the specified position, and the substring command can then be used to obtain the spacing as a string.
$spacing_t:=Substring($code; $foundPos_l; $lengthFound_l)

The spacing can then be applied to any new code in the current block being modified.
$newCode:= $spacing_t + "TRACE"