scan a string from right to left using PRX expression

Discussion in 'Perl Misc' started by Philip Primak, Aug 7, 2005.

  1. 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
     
    Philip Primak, Aug 7, 2005
    #1
    1. Advertising

  2. Philip Primak

    Guest

    "Philip Primak" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 7, 2005
    #2
    1. Advertising

  3. Philip Primak

    Peter Wyzl Guest

    <> wrote in message
    news:20050807012011.654$...
    : "Philip Primak" <> wrote:
    : > 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
     
    Peter Wyzl, Aug 7, 2005
    #3
  4. wrote:
    > "Philip Primak" <> wrote:
    >> scan a string from right to left using PRX expression.
    >> find the first space from the right in string "I love you".

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

    --
    With kind regards Veli-Pekka Tätilä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
     
    Veli-Pekka Tätilä, Aug 7, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liang
    Replies:
    2
    Views:
    1,768
  2. Sam
    Replies:
    3
    Views:
    14,201
    Karl Seguin
    Feb 17, 2005
  3. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    1,054
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  4. Oran
    Replies:
    2
    Views:
    607
  5. Replies:
    13
    Views:
    221
    robic0
    Aug 27, 2006
Loading...

Share This Page