Skip to content

CFG: Validation

Robert Jordan edited this page Jun 20, 2021 · 1 revision

CFG: Validation

Regex Patterns

Regular expressions to find and/or replace malformed CFG properties (as lines).


Basics

MATCH: Token character / Full token

[^ \t\r\n;]
[^ \t\r\n;]+

MATCH: Comment character / Full comment

;
;.*$

MATCH: Line whitespace / All whitespace

[ \t]
[ \t\r\n]

Patterns

Note: Two pattern choices are included for most sections below. The bottom pattern is available for Regex engines that do not support look-ahead or look-behind functionality.

FIND: Empty lines (including comments)

^(?=[ \t]*(?:;[^\r\n]*)?$)

^[ \t]*(?:;[^\r\n]*)?$

FIND: Invalid block braces (not properly spaced)

(?<=[^ \t\r\n;])[{}]|[{}](?=[^ \t\r\n;])

(?:[^ \t\r\n;])[{}]|[{}](?:[^ \t\r\n;])

FIND: Invalid // comments

(?<!;.*|[^ \t\r\n;])\/\/.*

^([^\r\n;]*[ \t])\/\/.*

REPLACE: Convert invalid // comments to ; comments

(?<!;.*|[^ \t\r\n;])\/\/
;

^([^\r\n;]*[ \t])\/\/
$1;

FIND: Lines with 3+ tokens

(?<=^[ \t]*)[^ \t\r\n;]+([ \t]+[^ \t\r\n;]+){2,}

^((?:^|[ \t]+)[^ \t\r\n;]+){3,}

FIND: Lines with 3+ tokens (excluding //)

(?<=^[ \t]*)(?!\/\/)[^ \t\r\n;]+([ \t]+(?!\/\/)[^ \t\r\n;]+){2,}

^((?:^|[ \t]+)(?:[^/ \t\r\n;]|/(?:[^/ \t\r\n;]|$))+){3,}

REPLACE: Comment out 3rd line tokens (strip // if present)

(?<=^[ \t]*(?:[^ \t\r\n;]+[ \t]+){2})(?=[^ \t\r\n;])(?:\/\/)?
;

^((?:(?:^|[ \t]+)[^ \t\r\n;]+){2}[ \t]+)(?:\/\/(.)|([^ \t\r\n;]))
$1;$2$3