How do I use Pat Commands to find last instance of a characterHi. I'm trying to use a Pat command to find the last instance of a character in a string. Additionally, I want to save all the characters following the last instance to a variable. For example, I want to find the last "t" in "Timothy the Tiger is taking his time." and then save "ime." to a variable since those characters follow the last instance of "t" in the string. Is there a way to do this using Pat commands?
I can resort to using the Contains() and Right() commands, but I was specifically wondering how to accomplish this more elegantly using Pat Commands.
Re: How do I use Pat Commands to find last instance of a characterThanks!
Re: How do I use Pat Commands to find last instance of a characterThe ".+t" matches any sequence of one or more characters that ends with "t". Since regex by default is greedy it matches the last "t" only.
".*t" matches zero or more chatacters ending with "t", which may be better here if the first letter was the only "t".
Re: How do I use Pat Commands to find last instance of a characterIn the Pat Regex command, what does the “.+t” do? Would this still work if I hadn’t ended the string with a “.”?
Re: How do I use Pat Commands to find last instance of a characterHere are two ways to do this. The first use Pat functions (which I regrettably rarely use), the second use Regex (which I'm more familiar with). There are certainly other ways to do this.
s = "Timothy the Tiger is taking his time.";
// With Pat functions
Pat Match(s, Pat Regex(".+t") + Pat Rem() >> last);
// Or the eqivalent with regular expressions
end = Regex(s, ".+t(.+)", "\1");
Show(last, end);
/*:
last = "ime.";
end = "ime.";