web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How can I find and rep...
Power Apps
Answered

How can I find and replace date in my text?

(0) ShareShare
ReportReport
Posted on by 7

For example: "on 12/12/2012, I have a cat and on 14/12/2012, I have a dog". I want to replace all date in my text. Thanks everyone!

Categories:
I have the same question (0)
  • GarethPrisk Profile Picture
    2,828 on at

    Those dates are text, correct? Meaning, they aren't coming from another date field and added to a string, but instead are part of the source text/string itself?

     

    https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-ismatch

     

    You'd be able to use a Match function, with an expression that checked for ##/##/####. Would your dates always be in this format?

     

    If so, then you'd be able to extract them into an array, and then use that array with a Replace or Substitute function to replace the dates. Would you know what the new date value was going to be?

  • HarutaNguyen1 Profile Picture
    7 on at

    Yes! The dates are text too. Those dates are read by a picture and convert string from picture to text

    And they always be in this format
    And I do not know the exactly date, you can understand them as random dates, but they are always in the future, not is the past.

    I have tried use for check the date like this: 

     

    If(IsMatch("12/12/2012", "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"),
    "Input OK",
    "Input must contain letters and numbers"
    )

     

    But if i add some text like " a dog 12/12/2012", my code will wrong. So, Can you help me? Thanks :((

  • Verified answer
    v-bofeng-msft Profile Picture
    on at

    Hi @HarutaNguyen1 :

    Could you tell me:

    • Is the function you need like this——

    45.gif

    If this is the case, there are several key points to achieve it

    • Find all characters in the string that meet the format of "##/##/####".
    • Identify the characters found (use the code you provided).
    • Replace the original string with keywords.

    I've made a test for your reference(If you are not interested in problem-solving ideas, you can directly look at the final solution at the bottom):

    Step1:Find all characters in the string that meet the format of "##/##/####".

    I used the following code:

     

    MatchAll("on 12/12/2012, I have a cat and on 14/12/2012, I have a dog","\S\S\/\S\S\/\S\S\S\S")

     

    1.JPG

    Step2:Identify the characters found (use the code you provided).

    I used the following code:

     

    AddColumns(
     MatchAll("on 12/12/2012, I have a cat and on 14/12/2012, I have a dog","\S\S\/\S\S\/\S\S\S\S"),
     "Input",
     If(
     IsMatch("12/12/2012", "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"),
     "Input OK",
     "Input must contain letters and numbers"
    ))

     

     2.JPG

    I added a new field "Input" to store the replacement value

    Step3:Replace the original string with keywords.

    If you need to implement all replacements, you must traverse the table created in the Step2 stage. However, the ForAll function has great limitations and cannot be used here. In the end, I chose to use the timer control to traverse the entire table.

    The final solution

    Add a timer control:

    OnSelect

     

    Set(
     var,
     "on 12/12/2012, I have a cat and on 14/12/2012, I have a dog"
    );/*Store the string to be processed in the variable var*/
    ClearCollect(
     mycollection,
     AddColumns(
     MatchAll(
     var,
     "\S\S\/\S\S\/\S\S\S\S"
     ),
     "Input",
     If(
     IsMatch(
     "12/12/2012",
     "^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
     ),
     "Input OK",
     "Input must contain letters and numbers"
     )
     )
    );/*Store the found target value and replacement value in the collection mycollection*/
    Set(
     Count,
     1
    )/*Count is a custom variable*/

     

    Duration

     

    500 /*loop in 0.5s*/

     

    OnTimerEnd

     

    Set(
     var,
     Substitute(
     var,
     Last(
     FirstN(
     mycollection,
     Count
     )
     ).FullMatch,
     Last(
     FirstN(
     mycollection,
     Count
     )
     ).Input
     )
    );
    Set(
     Count,
     Count + 1
    )

     

    Repeat

     

    Count<CountRows(mycollection) /*End the loop when Count>CountRows(mycollection)*/

     

    Finally, the processed string will be stored in the variable var

    Best Regards,

    Bof

  • HarutaNguyen1 Profile Picture
    7 on at

    Wow! Your solution exceeded my expectations. I will try. Thank you very much! 😁 

     
     
     
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard