pattern matching starting at end of string 
Author Message
 pattern matching starting at end of string

All,

I need to search for a pattern in a string but instead of starting the
search at the beginning of the string I need it to start at the end.
So if I had the string "myscriptmycode" and I wanted to search for the
pattern "my" I need it to find the second "my" in the string.  How can
I do this?  Can this be done through pattern matching or is there a
better way?  Any suggestions would be appreciated.  Thanks.



Tue, 16 Mar 2004 16:48:27 GMT  
 pattern matching starting at end of string

j> All,
j> I need to search for a pattern in a string but instead of starting the
j> search at the beginning of the string I need it to start at the end.
j> So if I had the string "myscriptmycode" and I wanted to search for the
j> pattern "my" I need it to find the second "my" in the string.  How can
j> I do this?  Can this be done through pattern matching or is there a
j> better way?  Any suggestions would be appreciated.  Thanks.

With regexps:

1. Use /.*my/

   .* will eat as much as possible chars this pattern will match last
   'my' string.

2. Intersting technique: reverse string and use reversed pattern
   (/ym/).

Using rindex:

1. rindex $str, 'my';

What should you use? It depends on what are you going to do with this
string.

--
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/)                                    |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80  E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/)                          |
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



Tue, 16 Mar 2004 17:10:56 GMT  
 pattern matching starting at end of string

Quote:
> I need to search for a pattern in a string but instead of starting
> the search at the beginning of the string I need it to start at the
> end.  So if I had the string "myscriptmycode" and I wanted to search
> for the pattern "my" I need it to find the second "my" in the
> string.  How can I do this?  Can this be done through pattern
> matching or is there a better way?  Any suggestions would be
> appreciated.  Thanks.

Two common ways are:

# allow a leading ".*" to gobble up the whole string so that the
# back-tracking can effectively perform a reverse search
$_ = "myscriptmycode";
/.*(my.*)/ and print "$1\n";

# reverse the string and the pattern
$_ = "myscriptmycode";
$_ = reverse;
/(.*?ym)/ and print scalar reverse($1), "\n";

Which one is better depends on the details.

--
Ren Maddox



Tue, 16 Mar 2004 17:03:28 GMT  
 pattern matching starting at end of string

Quote:
> I need to search for a pattern in a string but instead of starting the
> search at the beginning of the string I need it to start at the end.
> So if I had the string "myscriptmycode" and I wanted to search for the
> pattern "my" I need it to find the second "my" in the string.  How can
> I do this?  Can this be done through pattern matching or is there a
> better way?

If the pattern you a looking for is a constant string and not a regular
expression, then IMO rindex is the "better way". If it's a regular
expression, then rindex cannot, of course, be used.

Cheers,
Philip
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Wed, 17 Mar 2004 07:16:55 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Anchor matches at start/end of string?

2. Pattern matching from the end of a string

3. $ matches end-of-line *or* end-of-string!

4. matching words starting and ending with defined letters

5. Pattern Match - substitute a string after the match

6. to match pattern ended with 4 digits

7. HELP!! Pattern match stops before end of input

8. Patterns and end-of-string

9. Search pattern from right end of string

10. end of string matching

11. Matching ( Right to Left ) - Finding end of a String

12. Bug report: \Z sometimes matches BEFORE end of string

 

 
Powered by phpBB® Forum Software