cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
HarutaNguyen1
Frequent Visitor

How can I find and replace date in my text?

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!

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

4 REPLIES 4
GarethPrisk
Resident Rockstar
Resident Rockstar

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?

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 :((

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

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

 
 
 
 
 

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (2,456)