Split Multiline Text into Records

Aktualisiert: 9. Aug.


Or in other words:

Regex is your friend because the world of Regex is so powerful!

Wouldn't it be nice to copy and paste any text you like to save it directly into any table and it's splitted automatically?


It's annoying for users to split the lines on their own, right?


The full source code is available on github: https://github.com/byndit/BeyondAL/tree/main/BeyondAL/src/Editor



There are sevaral solutions out there to handle text lines in a much more better way than the standard:

You can use a wysiwyg editor or you store your text in blob fields or you leave it as it is.


Regular Expressions are so powerful, there are so many cases where you have to normalize or transform strings, unhandy text files or in our case split strings ;-).


Here are some examples how RegEx can be used:

“Your password must have at least 6 characters, an upper case letter, a lowercase letter, a number and probably a character."

Go for it:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W).{6,}$

“Your csv or text file is semicolon separated and you need to split line by line." There you go:

(?:^|;)(?=[^"]|(")?)"?((?(1)[^"]*|[^;"]*))"?(?=;|$)

"You need to valiadte the email address correctly."

Also available:

^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$

"Check for a valid phone no." Very cool:

^\+(?:[0-9] ?){1,13}[0-9]$

Anyway. Do you want to split a long text into multiple lines with a maximum length of 80 by?


In Dynamics 365 Business Central you can store comments at:

"G/L Account"
"Customer"
"Vendor"
"Item"
"Resource"
"Job"
"Resource Group"
"Bank Account"
"Campaign"
"Fixed Asset"
"Insurance"
"Nonstock Item"
"IC Partner"
"Vendor Agreement"
"Customer Agreement"


"Our scenario to split a string into multiple parts with a maximum length of 80 characters delimited by a space." Awesome:

(?![^\n]{1,80}$)([^\n]{1,80})\s

We just need to loop through all matches of this RegEx and get the first group!



So let's build a very simple pure al editor to load the comments from e.g. a customer and show it!


A Page of type "CardPart" could look like this:

    layout
    {
        area(content)
        {
            field(EditorText; Msg)
            {
                ApplicationArea = All;
                MultiLine = true;
                ShowCaption = false;
                ExtendedDatatype = None;
            }
        }
    }

This is how we load all line from the comment line table into one string called "Msg":

        Found := CommentLine.Find(Which);
        if Found then
                repeat
                    if Msg = '' then
                        Msg := CommentLine.Comment
                    else
                        Msg += GetLF() + CommentLine.Comment;
                until CommentLine.Next() = 0;

And this is how we split the sting in our variable "Msg" into many:

TempGroups: Record Groups temporary;
TempMatches: Record Matches temporary;
RegEx: Codeunit Regex;
RegExPattern: Text;

RegExPattern := '(?![^\n]{1,80}$)([^\n]{1,80})\s';
RegEx.Match(Msg, RegExPattern, TempMatches);

if TempMatches.IsEmpty() then
    exit;

TempMatches.find('-');
repeat
    RegEx.Groups(TempMatches, TempGroups);
    TempGroups.Get(1);
    LineNo += 10000;
    CommentLine."No." := No;
    CommentLine."Table Name" := CommentLineTableName;
    CommentLine.Comment := copystr(TempGroups.ReadValue(), 1, 80);
    CommentLine."Line No." := LineNo;
    CommentLine.Date := Today();
    CommentLine.Insert(true);
until TempMatches.Next() = 0;

And you are fine!

Get the full repo here:

https://github.com/byndit/BeyondAL/tree/main/BeyondAL/src/Editor



271 Ansichten0 Kommentare

Aktuelle Beiträge

Alle ansehen