CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
extracting substrings

 



jacqui
Novice

Feb 7, 2000, 2:26 AM

Post #1 of 3 (706 views)
extracting substrings Can't Post

How can I extract everything to the right of the nth occurance of x in one string to another string?

e.g.

$source = "the-cat-sat-on-the-mat"
$occurance = 3
$result = "on-the-mat"

Thanks


Jasmine
Administrator / Moderator

Feb 7, 2000, 12:06 PM

Post #2 of 3 (705 views)
Re: extracting substrings [In reply to] Can't Post

Straight out of the faq...

You have to keep track of N yourself. For example, let's say you want to change the fifth occurrence of "whoever" or "whomever" into "whosoever" or "whomsoever", case insensitively.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$count = 0;
s{((whom?)ever)}{
++$count == 5 # is it the 5th?
? "${2}soever" # yes, swap
: $1 # renege and leave it there
}igex;
</pre><HR></BLOCKQUOTE>

In the more general case, you can use the /g modifier in a while loop, keeping count of matches.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$WANT = 3;
$count = 0;
while (/(\w+)\s+fish\b/gi) {
if (++$count == $WANT) {
print "The third fish is a $1 one.\n";
# Warning: don't `last' out of this loop
}
}
</pre><HR></BLOCKQUOTE>

That prints out: "The third fish is a red one." You can also use a repetition count and repeated pattern like this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


/(?:\w+\s+fish\s+){2}(\w+)\s+fish/i;
</pre><HR></BLOCKQUOTE>

To adapt the above to your needs, you can alter the above to something like this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$WANT = 3;
$count = 0;
while (/(\w+)\s+fish\b/gi) {
if (++$count == $WANT) {
$result = (split(/$1/,$_))[1];
# Warning: don't `last' out of this loop
}
}
</pre><HR></BLOCKQUOTE>

I haven't tested it, but you get the idea Smile

Good luck!


[This message has been edited by Jasmine (edited 02-07-2000).]


jacqui
Novice

Feb 8, 2000, 2:53 PM

Post #3 of 3 (706 views)
Re: extracting substrings [In reply to] Can't Post

Jasmine,
Thanks for your help, but I don't want to change the original string, I want to extract a trailing section of it to another string.

Reading my original post again I realised I may not have described the problem properly.

The length of the source will be variable, the delimiter between the words can be variable (sometimes a space, other times a tab or pipe or comma etc) and the section I am trying to extract can also be variable depending on the delimiter. Just to make life really interestng the occurance count must start from the right.

e.g. If the delimiter is a pipe then extract the last 3 words, if it's a comma only extract the last two words.

The only way I can think of is to split the source string to an array, pop it as many times as needed and create the result string bit by bit but it seems a clumsy way of doing it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives