Remove characters from strings using Regex in Power Apps

You may also like...

5 Responses

  1. 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:

      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:

      Character1: Last(FirstN(Split(TextInput1.Text,””),Value)).Result,
      Character2: If(Value = CountRows(Split(TextInput1.Text,””)),
      Last(FirstN(Split(TextInput1.Text,””),Value +1)).Result
      PreviousChar1: If(Value = CountRows(Split(TextInput1.Text,””)) || (Value=0),
      Last(FirstN(Split(TextInput1.Text,””),Value -1)).Result)
      CharacterEvaluation: If(IsMatch(Character1 & Character2, “([\\])([n])”), Char(10),
      !(Value=0) && IsMatch(PreviousChar1 & Character1, “([\\])([n])”), Char(10),
      If(!(CharacterEvaluation = “”), CharacterEvaluation)

      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

  2. 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 *