scan a string from right to left using PRX expression

P

Philip Primak

Hi


I wonder if there is a way to scan a string from right to left using
PRX expression. For example I need to find the first space from the
right in string "I love you". So I want the pattern / / scan from right
to left and find as result space between "love" and "you" at position
7. I understand that it can be solved using pattern / [^ ]*$/ or using
strait pattern / / and trick like
position=length(string)-prxmat­ch(prx,reverse(string))+1 or something
like this. All those solutions do not force PRX functions to scan from
right to left but they just use tricks to get the last space found. I
am just curious if there is a option which would force PRX expressions
to make scan from right to left.


Thank you
Philip Primak
Genzyme Corp
 
X

xhoster

Philip Primak said:
Hi

I wonder if there is a way to scan a string from right to left using
PRX expression. For example I need to find the first space from the
right in string "I love you".

I don't know what PRX is, but it seems like you could just reverse the
string before sending it to PRX.

Thank you=20
Philip Primak=20
Genzyme Corp

Xho
 
P

Peter Wyzl

: > Hi
: >
: > I wonder if there is a way to scan a string from right to left using
: > PRX expression. For example I need to find the first space from the
: > right in string "I love you".
:
: I don't know what PRX is, but it seems like you could just reverse the
: string before sending it to PRX.

<guess> Perl Regular eXpression </guess>

P
 
V

Veli-Pekka Tätilä

I don't know what PRX is,
Me neither.
you could just reverse the string before sending it to PRX.
Or howabout using substr to extract part of a string and rindex to find the
last occurrence of a sub-string in a string.

Sample code:
use strict;
use warnings;
my $string = 'I love you';
print "The string is '$string'\n";

my $lastSpace = rindex $string, ' '; # string, substring, [offset]
print "The $[ based character index of last space is $lastSpace\n";

# find everything from last space onwards (including last space).
my $lastWord = substr $string, $lastSpace; # string, offset, [length]
print "text from $lastSpace to ", length($string) - 1, " is '$lastWord'\n";

Output:
The string is 'I love you'
The 0 based character index of last space is 6
text from 6 to 9 is ' you'

Hope this can be of help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top