Remove characters from strings using Regex in Power Apps

You may also like...

11 Responses

  1. David Powell says:

    I constantly have folks copying and pasting from Excel…I need to get rid of all characters except digits ? i.e. I guess spaces carriage returns…etc..
    Can Regex do that ?
    Thanks great article

    • iAm_ManCat says:

      Hi David,

      Absolutely, If you use the code in the “Change the text AS YOU TYPE” subheading, you can modify the Regex string to only allow through the numbers that were pasted using “([0-9])”, like this:

      Set(gblTextRegex, Concat( Split(TextInput1.Text, “” ), If( IsMatch(Value, “([0-9])” ), Value))); Reset(TextInput1)

      Cheers,
      Sancho

  2. Michael Wan says:

    Great article! When I put in the Concat( Split(Label1.Text, “” ), If( IsMatch(Result, “([A-Za-z0-9 ])” ), Result ) ), I got an error that Result is not defined

    • iAm_ManCat says:

      Hey Michael!

      Microsoft stealth-updated the Split and Distinct functions to use Value instead of Result (I linked to the Power Apps version notes where they did that)

      I have just updated the article with the newer formulae – let me know if any of them are still giving you trouble!

      Cheers,
      Sancho

  3. Nathan Boothe says:

    I must be doing something really stupid because I can’t get past step 2. I create the two labels and when I try Split(Label1.Text, “”) I get the error “Expected Text Value”. I even tried replacing Label1.Text with an actual text string and get the same error. What am I doing wrong?

    • iAm_ManCat says:

      Hey Nathan,

      As it says right above that – it creates a Table of records, not a text value, the next step is to reconstitute that table of values back into text using Concat 🙂
      Cheers,
      Sancho

  4. Ed Mpanduki says:

    Thank you for this post this is something I’ve been looking for. I was wondering is there a way to remove a combination of characters? If I have text like below, I want to remove ‘\n’, where I try Concat(Split(Label1.Text,””),If(IsMatch(Result,”([^\\n])”),Result)), it removes all the n’s as well

    Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book

    • iAm_ManCat says:

      Hi,
      Yes absolutely, I think if you prepare a variable that can be manipulated then you can separate that into groups of two by defining character 1 and 2 in a sequence.
      So replace the timer code in the last step of the blog with the following:

      Set(gblSplitArrayNumbered,
      Concat(
      ForAll(
      Sequence(CountRows(Split(TextInput1_1.Text,””))),
      With(
      {
      Character1: Last(FirstN(Split(TextInput1.Text,””),Value)).Result,
      Character2: If(Value = CountRows(Split(TextInput1.Text,””)),
      Blank(),
      Last(FirstN(Split(TextInput1.Text,””),Value +1)).Result
      ),
      PreviousChar1: If(Value = CountRows(Split(TextInput1.Text,””)) || (Value=0),
      Blank(),
      Last(FirstN(Split(TextInput1.Text,””),Value -1)).Result)
      },
      {
      CharacterEvaluation: If(IsMatch(Character1 & Character2, “([\\])([n])”), Char(10),
      !(Value=0) && IsMatch(PreviousChar1 & Character1, “([\\])([n])”), Char(10),
      Character1)
      }
      )
      ),
      If(!(CharacterEvaluation = “”), CharacterEvaluation)
      )
      );
      Reset(TextInput1_1);

      I tested that with some Lorem and some other random added \n and it pulled them out of the string 🙂 You can of course modify that code to have it do something different if \n is found like insert a Char(10) new line

  5. Valerie Schaffer says:

    Great way of doing that! Thank you. I can’t tell if that last one allows hyphens or apostrophes, though. It’s important to allow those two, especially in a name field for those people who have hyphenated names or Irish names like O’Reilly.

    • iAm_ManCat says:

      Hi Valerie!
      The formula there only covers capitals, lowercase, numbers and spaces. Normally we’d be using that kind of formula to ensure there are no special characters like hyphens or apostrophes so that when we have to pass that data to something that needs to create folders for example, then it will not fail due to the presence of special characters.

      If you want to validate characters for names and include the hyphen and apostrophe characters you can simply add them to the regex string array (you can use the following Regex string in place of the one listed):
      “[A-Za-z0-9 ‘-]”

Leave a Reply

Your email address will not be published. Required fields are marked *