Regular Expressions in X++ for D365 Finance and Operations

Regular Expressions in X++ for D365 Finance and Operations

Hello everyone,

I would like to share some insights about using regular expressions in X++ for D365 Finance and Operations. We usually rely on various string functions when working with string functions for manipulating strings and replacing text. However, instead of using multiple string functions, we can achieve the desired results with a single method using regular expressions. Although X++ supports only a few regular expressions, they can significantly simplify and enhance our string manipulation tasks.

Expression 1: Match(Str _Value, Str _Pattern)

The Match method is used to find an exact pattern match in a given string. This method takes two parameters:

  1. _Value: The string in which you want to search for the pattern.
  2. _Pattern: The regular expression pattern you want to match.

The return type of this method is System.Text.RegularExpressions.Match, which contains information about the match, including the matched value and its position in the original string.

System.Text.RegularExpressions.Regex::Match(__Value,_Pattern)

Ex:1
public  static void GetExpressionValue()
{
 System.Text.RegularExpressions.match myMatch;
Str Value ="Assigned to user: ravi.krishna Due date 6/25/2024 08:23:00 am";
// To get date time value use below pattern DD/MM/YYYY HH:MM:SS AM
str dueDate = @"[0-9]{1,2}([\-/ \.])[0-9]{1,2}[\-/ \.][0-9]{4}([\-/ \.])[0-9]{2}[:][0-9]{2}[:][0-9]{2}[\-/ \.][a-zA-Z]{2}" ;
                   
                 myMatch = System.Text.RegularExpressions.Regex::Match(Value,dueDate);
                while (myMatch.Success)
                        {
                            info(myMatch.Value);
                           myMatch = myMatch.NextMatch();
                        }
}

output: 6/25/2024 08:23:00 am
Ex :2
public  static void GetExpressionValue()
{
 System.Text.RegularExpressions.match myMatch;
Str Value ="Assigned to user: ravi.krishna. Due date 6/25/2024 08:23:00 am";
// To get the value between user: and due date string use below pattern
str  names =@"(\S+)\.";
                    myMatch = System.Text.RegularExpressions.Regex::Match(Value,dueDate);
                while (myMatch.Success)
                        {
                            info(myMatch.Value);
                           myMatch = myMatch.NextMatch();
                        }
}
 output: ravi.krishna.        

Expression 2: Matches(Str _Value, Str _Pattern)

The Matches method is similar to the Match method but with an important difference: it searches an input string for all occurrences of a regular expression and returns all the matches. This method returns a collection of System.Text.RegularExpressions.MatchCollection objects found by the search. If no matches are found, the method returns an empty collection object.

However, it is important to note that X++ does not support the Matches method; it works in C#.

System.Text.RegularExpressions.Regex::Matches(_Value,_Pattern)

public  static void GetExpressionValue()
{
 System.Text.RegularExpressions.MatchCollection  myMatchCol;
Str Value ="Assigned to user: ravi.krishna Due date 6/25/2024 08:23:00 am Due date";
// To get Due date string use below pattern
str dueDate =  @"\b(Due date)\b";
                   
                 myMatchCol = System.Text.RegularExpressions.Regex::Matches(Value,dueDate);
                    info(strfmt(" Result-%1 count-%2"myMatchCol.Tostring(),myMatchCol.count));
                         
}
 Expected output :  Result-Due date count-2
Note: X++ throwing error while using matches expression         

Expression 3: IsMatch(Str _Value, Str _Pattern)

The IsMatch method returns a boolean value indicating whether a given pattern exists in a specified string. This method is useful for validating whether the given pattern exists or not. System.Text.RegularExpressions.Regex::IsMatch(_Value,_Pattern)

public  static void GetExpressionValue()
{
 boolean myMatch;
str Value ="Assigned to user: ravi.krishna. Due date 6/25/2024 08:23:00 am";
// To get the value between user: and due date string use below pattern
str  names =@"(\S+)\.";
                    myMatch = System.Text.RegularExpressions.Regex::IsMatch(Value,names);
                 info(myMatch);
if(myMatch)
{
//
}
                         
}
 output: true        

Expression 4: Replace(Str Value, Str Pattern, Str _ReplaceStr)

The Replace method is used to replace occurrences of a specified pattern in a given string with a replacement string.

System.Text.RegularExpressions.Regex::Replace(_Value,_Pattern,_ReplaceStr)

This method takes three parameters:

  1. _Value: The string in which you want to perform the replacement.
  2. _Pattern: The regular expression pattern you want to replace.
  3. _ReplaceStr: The string to replace the pattern with.

The return type of this method is str, which is the modified string after replacements.

public  static void GetExpressionValue()
{
 str myMatch;
str Value ="Assigned to user: ravi.krishna Due date 6/25/2024 08:23:00 am";
// To get date time value use below pattern DD/MM/YYYY HH:MM:SS AM
str dueDate = @"[0-9]{1,2}([\-/ \.])[0-9]{1,2}[\-/ \.][0-9]{4}([\-/ \.])[0-9]{2}[:][0-9]{2}[:][0-9]{2}[\-/ \.][a-zA-Z]{2}" ;
                   
                 myMatch = System.Text.RegularExpressions.Regex::Replace(Value,dueDate,"*");
                
                            info(myMatch);
 }
output : Assigned to user: ravi.krishna Due date *        

Expression 5: Split(Str _Value, Str _Pattern)

The Split method is used to split an input string into an array of substrings at the positions defined by a regular expression pattern. This method takes two

System.Text.RegularExpressions.Regex::Split(_Value,_Pattern)

parameters:

  1. _Value: The string you want to split.
  2. _Pattern: The regular expression pattern to define where to split the string.

The return type of this method is String[], which is an array of substrings. However, it is important to note that X++ does not support the Split method as it returns a string array, which leads to an error in X++.

public  static void GetExpressionValue()
{
 System.string[] mySplit;
Str Value ="D365Fno -D365Fno";
str dueDate =  @"-";
        mySplit = System.Text.RegularExpressions.Regex::Split(Value,dueDate);
 for (int i = 0; i< mySplit.Length; i++) 
{
         info(strfmt(" Result-%1 mySplit.Value));
}
 expected otput :  Result-D365Fno
                               Result -D365Fno
Note: X++ throwing error while using declaring System.string[] string array         

Expression 6: Escape(Str _Value)

The Escape method is used to escape characters in a string that have special meaning in regular expressions. By prefixing these characters with a backslash (\), you ensure they are treated as literal characters in the pattern. This method takes

System.Text.RegularExpressions.Regex::Escape(_Value)

one parameter:

  1. _Value: The string containing characters that need to be escaped.

The return type of this method is str, which is the modified string with escaped characters.

public  static void GetExpressionValue()
{
str myMatch;
str Value ="Assigned to user: ravi.krishna. Due date 6/25/2024 08:23:00 am";

                 myMatch = System.Text.RegularExpressions.Regex::Escape(Value)
                        
                            info(myMatch);
     }
output: Assigned\ to\ user:\ ravi\.krishna\.\ Due\ date\ 6/25/2024\ 08:23:00\ am\         

Expression 7: Unescape(Str System.Text.RegularExpressions.Regex::Unescape(Value))

The Unescape method is used to reverse the process of the Escape method. It converts any escaped characters in a string back to their literal representations.

System.Text.RegularExpressions.Regex::Unescape(_Value)

This method takes one parameter:

  1. _Value: The string containing escaped characters that need to be converted back to literal characters.

The return type of this method is str, which is the modified string with unescaped characters.

public  static void GetExpressionValue()
{
str myMatch;
str Value ="Assigned to user: ravi.krishna. Due date 6/25/2024 08:23:00 am";

                 myMatch = System.Text.RegularExpressions.Regex::Unescape(Value)
            
                            info(myMatch);
      }
output: Assigned to user: ravi.krishna. Due date 6/25/2024 08:23:00 am        

Please refer below link to get more understanding about how to use different patter according to our string value.

Regular Expression Language - Quick Reference - .NET | Microsoft Learn

要查看或添加评论,请登录

Ratna P的更多文章

社区洞察

其他会员也浏览了