Overview
The pattern you are using, From:.*<
, employs the *
quantifier which is known as a greedy quantifier. This means that it will match any character any number of times and will continue matching as many characters as possible to satisfy the pattern. In your case, it will match up until the last occurrence of <
because of how this greedy quantifier operates.
Code
Here are a couple of methods you can utilize to match up to the first occurrence of <
(after From:
)
Lazy Quantifier
This method involves making the quantifier *
lazy, meaning it will match any character any number of times but only as few as needed. Microsoft's documentation on Backtracking in Regular Expressions explains this concept well:
When a regular expression includes optional quantifiers or alternation
constructs, the evaluation of the input string is no longer linear. [...] Therefore, the regular expression engine tries to
fully match optional or alternative subexpressions. When it advances
to the next language element in the subexpression and the match is
unsuccessful, the regular expression engine can abandon a portion of
its successful match and return to an earlier saved state in the
interest of matching the regular expression as a whole with the input
string. This process of returning to a previous saved state to find a
match is known as backtracking.
View this regex example here
From:.*?<
Negated Character Set
This method uses a negated character set to match any character except the specified character (in this case, <
). It is often considered more efficient than using the lazy quantifier since it does not involve backtracking, resulting in better performance. According to regex101, the Lazy Quantifier method takes 23 steps to match your string while this method requires only 11 steps for the same match.
Check out the regex in action here
From:[^<]*<