cancel
Showing results for 
Search instead for 
Did you mean: 

IsMatch() show allow for variable (not constant) regular expressions

IsMatch() should allow the ability to use a variable (global and context) for the Pattern parameter (2nd) that contains the regular expression pattern.   Currently IsMatch() function requires the regular expression (2nd parameter) to be a constant value (literal string).

 

ie: the request is that the below should be permissible:

 

Set( URL_MATCH, "(ht|f)tp(s?)://[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/\+&%$#_]*)?" );

Set( IsUrl, IsMatch( TextInput1.Text, URL_MATCH ) )

(Note the forum here converts part of the regular expression in the code section above into a frown face, whereas that section really should be a colon : followed by a left paren ( - the expression I attempted to put here is the same in IsMatch() for identifying a URL)

 

Currently, this results in the error:

 

   The function 'IsMatch' has some invalid arguments.

   Regular expressions must be constant values.

 

Why must this be constant?  Seems that this is an unnecessary limitation.

 

Issues with this approach (requiring these to be constant) include:

 

  1. Inability to define the RegEx expresions in a single place in the code (setting in a global variable) and use throughout the application (probably issue #1 overall with PowerApps is limitations on code reuse and having to duplicate unnecessarily code).  Currently I have to copy IsMatch() expressions throughout my code with the SAME literal string everywhere (of course this could be helped if we had Macro support in PowerApps...)"

  2. Inability to read RegEx expressions from an external source - ie: assume the requirement is to allow the expression to be changed external to the application, and the application (on startup) read these in.  This is a realistic requirement.

 

Please consider modifying IsMatch() to allow the "Pattern" parameter to be a string.  

 

Note that since PowerApps does not have true definable constants, the only way to do this is with a literal string, ie: 

   "(ht|f)tp(s?)://[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/\+&%$#_]*)?"


which does not promote code reusability - and thus the only way to use IsMatch() is with a literal string.

 

However, even with support for true contants or macros - neither of which are available in PowerApps (which may be one way to address #1), issue #2 is still not addressed.  

 

Allowing for a variable to be used for Pattern would not only promote code reusability to some degree (#1 - since I could just set my patterns in variables early in the code like in OnStart()), but also allow for much more maintainable applications and more flexibility and address #2.

 

BTW - also note that the documentation does NOT address this limitation of IsMatch() (ie: that Pattern must be a constant)

Status: Under Review

Upgrading to In Review, adding @LanceDelano and @GregLi who are looking at improvements in this area

Comments
Anonymous
Not applicable

This should really be possible

JR-BejeweledOne
Super User

I have to really stress this.   As a comparison operator, IsMatch should take a variable input, otherwise its uses are very limited.  To make it a truly valuable comparison operator it needs to be able to be used with a variable in addition to the static patterns.

Ianbudson
Helper I

I just came across this limitation and am now stumped on an app.......why can't it use a variable?????

GavinM
Advocate II

Just working on a forms and workflow app that uses metadata to build the form.  I thought that using a flexible regex would be a great way to validate data ... amazed to find that it had to be a literal string!

 

Is anything being done on this?  IsMatch not being able to take a variable for the pattern really limits its capability.

TxH
Advocate IV
Advocate IV

For IsMatch(), there is a workaround available:

you can receive the expression as string output from a component where you can build the string as a combination of strings and other data.

 

Let me show you an example:

 1. Enable the Experimental setting Enhanced component properties

 2. Create a new Component and name it myregex

 3. Create a New custom property getExpression in it (Property type: Output, Data type: Text) 

 4. Add a New parameter of type Text (this will not be used but is necessary to define the property as a function

image.png

5. Type the following code in getExpression 

With({ expr: "\w*" },
    "\s*" & expr
)

6. Go back to Screens and add the component to it (name will be myregex_1)

7. Place a new Label control to the screen

8. Type the following code in the Default property of the Label control:

With( { digit: "\d" },
    // supported
      IsMatch("string", myregex_1.getExpression())
    /*
    not supported
      IsMatch("string", digit & "\s*\w*")
      IsMatch("string", digit & myregex_1.getExpression())
      IsMatch("string", myregex_1.getExpression(digit))
    */
)

Voila!, the expression is a combination of strings and constants; the Label shows the value true.

 

For any unknown reason this only works for IsMatch(), not for Match() and MatchAll().

It would be very much needed that Microsoft enables this functionality also for Match() and MatchAll()

 

 

 

 

 

 

JAWL
Super User

I believe this idea is under review.. btw any workaround worth trying at this juncture? I urgently need to compare two strings without case sensitive..

AndrewLansford
Frequent Visitor

@TxH this worked perfectly!

 

A handful of changes make this truly dynamic.

1. Add an input property "setPattern" to the component 

AndrewLansford_0-1652811502977.png

 

2. Update the getPattern output property to rely on Self.setPattern

AndrewLansford_1-1652811626548.png

 

3. In the app when using the component, the setPattern is a formula that does a LookUp or is otherwise variable.

 

Thanks for the work-around. Hopefully this one doesn't get plugged when they officially release component parameters.

micross
Frequent Visitor

Is there any update on this?

takolota
Super User

I could definitely use this to make a nice JSON parser that automatically adjusts the regex to whatever column names are coming in and adjusts to different datatypes.

I mean, I have another way of doing it as long as the app developer manually inputs the exact column/key names in a couple Regex expressions, but it would just be really nice from a developer user-friendliness & readability perspective.

ChristopherU
Frequent Visitor

I find it very bizarre that this problem hasn't been addressed. I run into it every time I create a canvas app. Is it uncommon to need to compare the values of two variables?