Ahhh Regular Expressions… that thing that keeps you returning to Stack Overflow for answers every time you need to use this language:
Even so, this language is extremely helpful when you need a more powerful way to deal with string data.
Recently, Power Apps got an update to add more features to it’s regular expression functions. We now have to two more functions in our ever growing arsenal: Match and MatchAll.
As the name implies, these functions serve mainly to find matches for a given regular expression formula. Match finds the first piece that meets the requirements and MatchAll gives us a table of records of all the matches. This is just a brief explanation of these functions, refer to the documentation to get more insight.
How is this used in PowerTwitter?, well you might have noticed that the tweet messages have the hashtags and mentions in different colors:
Well, to accomplish this, I’ve re-formatted the contents of the tweet messages to change the style of these hashtags and mentions to match the Accent theme color. This is done by applying CSS to an HTML control.
I’ve could have used the Hashtags function but that would only give us the hashtags and not the mentions. That’s why I’ve opted to include this new functions.
Working with collections
If you downloaded PowerTwitter from my GitHub repo you might have noticed that the Twitter’s timeline is being stored in a collection called Timeline. This collection has all the data needed to properly display all the tweets.
I’ve then extracted the hashtags and mentions using this code:
ClearCollect(
Tags,
MatchAll(
Concat(
Timeline,
TweetText
),
"#\w+"
).FullMatch
);
Collect(
Tags,
MatchAll(
Concat(
Timeline,
TweetText
),
"@\w+"
).FullMatch
);
By using MatchAll in conjunction with the Concat function, I can join all the tweet messages and then extract the hashtags ( #\w+ which translates into hashtag symbol and a word ) and the mentions ( @\w+ which translates into @ symbol and a word ) into a collection called Tags.
Now, we need to update the messages in the Timeline collection with the corresponding CSS style. This is were things get tricky.
To accomplish this, I’ve used the ForAll function which somehow iterates a collection by performing actions on all records. The next step would be to update the collection but because one tweet message could have more than one hashtag and mention, we need to use some LookUps in the process.
The thing is that you can’t update the collection being used in a ForAll function:
So, as a workaround, I’ve created an index collection called Temp by gathering the TweetId from the Timeline collection:
ClearCollect(
Temp,
ForAll(
Timeline,
{Id: TweetId}
)
);
This way I can iterate over the Temp collection and then, iterate over all the distinct tags found so I can update the Timeline collection using Patch.
ForAll(
// Temp collection used to iterate
Temp,
ForAll(
// Only return the unique tags
Distinct(
Tags,
FullMatch
),
Patch(
Timeline,
LookUp(
Timeline,
TweetId = Id
),
{
TweetText: Substitute(
// We need to get the actual tweet message here
// in case one tweet message has more than one
// hashtag or mention
LookUp(
Timeline,
TweetId = Id
).TweetText,
// Result is the matched tag
Result,
"<font color=#1b95e0>" & Result & "</font>"
)
}
)
)
)
Lastly, as the ForAll documentation explains:
keep in mind that records can be processed in any order and, when possible, in parallel.
Because of this, we need to use the LookUp function when patching the tweet message so we make all the changes necessary no matter how many hashtags or mentions the message has.
I hope this helps you overcome this type of scenarios where you might need to update one collection with another.
See you soon!