NAV Regular Expression - Match Transformation

jordi79jordi79 Member Posts: 275
Hi,
I notice something peculiar with the NAV Standard RegEx - match found in Page Transformation Rule Card (1238). This NAV standard RegEx Match skips the 1st match result.
e.g.

tkr57i7v6kz3.png

I expect to see
data: 1
data: 2
data: 3
data: 4
data: 5

As match results. But NAV RegEx match skips the 1st match result.

When the same Regex expression in tested in:
regexr.com/73md2

It matches all 5 lines.





Best Answer

  • vaprogvaprog Member Posts: 1,140
    Answer ✓
    That function is not designed for a multi line string.
    The way it is designed it expects one match. It then skips the first capture of the match group (which is the entire match). It then returns a concatenation of the capture groups of the regex. (You do not have any capture groups in your regex). Instead, what you get is the remaining matches as a whole.

    If you added a capture group for wat you probably expect to get back, like so:
    (?=.*data)(.*)
    
    the result of the transformation would be
    data: 1data: 2data: 2data: 2data: 3data: 3data: 4data: 4data: 5data: 5
    
    (= first capture group of first match + 2nd match + first capture group of 2nd match + ...)

    You will not get the line breaks in the result, unless you change the options, as the dot operator does not match line break characters by default.

    Note: the terminology might be confusing, as "capture group" above reffers to the element of the regular expression, not to some .NET data structure / class

Answers

  • vaprogvaprog Member Posts: 1,140
    Answer ✓
    That function is not designed for a multi line string.
    The way it is designed it expects one match. It then skips the first capture of the match group (which is the entire match). It then returns a concatenation of the capture groups of the regex. (You do not have any capture groups in your regex). Instead, what you get is the remaining matches as a whole.

    If you added a capture group for wat you probably expect to get back, like so:
    (?=.*data)(.*)
    
    the result of the transformation would be
    data: 1data: 2data: 2data: 2data: 3data: 3data: 4data: 4data: 5data: 5
    
    (= first capture group of first match + 2nd match + first capture group of 2nd match + ...)

    You will not get the line breaks in the result, unless you change the options, as the dot operator does not match line break characters by default.

    Note: the terminology might be confusing, as "capture group" above reffers to the element of the regular expression, not to some .NET data structure / class
  • jordi79jordi79 Member Posts: 275
    I saw the code on how it was written in NAV for the standard NAV RegExMatch, and I cannot understand what is the practical reasons on why it was written that way.

    I ended up writing a simpler custom transformation, and it ended up behaving like how https://regexr.com/ behaves.
    dnMatches := dnRegEx.Matches(SearchString,TransformationRule."Custom Parameter 1");
    FOR i := 0 TO dnMatches.Count - 1 DO BEGIN
    OutputText += dnMatches.Item(i).Value;
    END;
Sign In or Register to comment.