Choose Language Hide Translation Bar
Highlighted
nikles
Level V

How do I use Pat Commands to find last instance of a character

Hi.  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. 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
ms
Super User ms
Super User

Re: How do I use Pat Commands to find last instance of a character

Here 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.";

 

 

View solution in original post

Highlighted
ms
Super User ms
Super User

Re: How do I use Pat Commands to find last instance of a character

The ".+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".

 

 

View solution in original post

4 REPLIES 4
Highlighted
ms
Super User ms
Super User

Re: How do I use Pat Commands to find last instance of a character

Here 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.";

 

 

View solution in original post

Highlighted
nikles
Level V

Re: How do I use Pat Commands to find last instance of a character

In the Pat Regex command, what does the “.+t” do? Would this still work if I hadn’t ended the string with a “.”?
Highlighted
ms
Super User ms
Super User

Re: How do I use Pat Commands to find last instance of a character

The ".+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".

 

 

View solution in original post

Highlighted
nikles
Level V

Re: How do I use Pat Commands to find last instance of a character

Thanks!

Article Labels

    There are no labels assigned to this post.