Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gregli-msft committed Mar 7, 2025
1 parent ea86e02 commit 1abd009
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/regular-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ As a result, some regular expressions that may work in other environments will b

Power Fx supports the following regular expression features, with notes on how Power Fx behavior may differ from other systems.

The regular expression must be a constant and not calculated or stored in a variable. Using the `&` operator and the `Concatenate`, `Char`, and `UniChar` functions with constant arguments is supported.
The regular expression must be a constant and not calculated or stored in a variable. Using the `&` operator, string interpolation `$"{...}"`, and the `Concatenate`, `Char`, and `UniChar` functions with constant arguments is supported.

### Literal characters

Expand Down
22 changes: 20 additions & 2 deletions src/libraries/Microsoft.PowerFx.Core/Binding/BinderUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.PowerFx.Core.App.Controls;
using Microsoft.PowerFx.Core.App.ErrorContainers;
Expand Down Expand Up @@ -1504,7 +1505,24 @@ public static bool TryGetConstantValue(CheckTypesContext context, TexlNode node,
Contracts.AssertValue(node);
nodeValue = null;
switch (node.Kind)
{
{
case NodeKind.StrInterp:
var strInterpNode = node.AsStrInterp();
StringBuilder strInterpValue = new StringBuilder();
foreach (var segmentNode in strInterpNode.Children)
{
if (TryGetConstantValue(context, segmentNode, out var segmentValue))
{
strInterpValue.Append(segmentValue);
}
else
{
break;
}
}

nodeValue = strInterpValue.ToString();
return true;
case NodeKind.StrLit:
nodeValue = node.AsStrLit().Value;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,19 @@ Errors: Error 17-31: Regular expression must be a constant value.|Error 0-7: The
>> IsMatch( "asdf", "asdf", If( Int(4) > -1, MatchOptions.IgnoreCase, MatchOptions.Contains ) )
Errors: Error 25-90: MatchOptions must be a constant value.|Error 0-7: The function 'IsMatch' has some invalid arguments.

// String interpolation is supported
>> Match( "asdf", $"a{ Char(115) }{ UniChar(100) }f" )
{FullMatch:"asdf",StartMatch:1}

>> Match( "asdf", $"a{$"s{$"d{$"f"}"}"}" )
{FullMatch:"asdf",StartMatch:1}

>> Match( "asdf", $"a{ Char(115) }" & $"{ UniChar(100) }f" )
{FullMatch:"asdf",StartMatch:1}

>> Match( "asdf", "a" & $"{Char(115)}" & $"{UniChar(100)}" & "f" )
{FullMatch:"asdf",StartMatch:1}

// leading zeros on quant support

>> Match( "aaaaaa", "a{00002,}" )
Expand Down

0 comments on commit 1abd009

Please sign in to comment.