beginners question

Discussion in 'Perl Misc' started by Alpha, Sep 8, 2006.

  1. Alpha

    Alpha Guest

    Hi guys, n00bs here.

    I was playing around with regexp and got stuck with this:
    $string ="String 12345 String 67890 String whatever here";
    basically i want to strip off the last "String" and whatever that comes
    after that.
    so $string will become "String 12345 String 67890 ";

    any hints will be appreciated.

    Thanks,
    Alpha
     
    Alpha, Sep 8, 2006
    #1
    1. Advertising

  2. Alpha

    David Squire Guest

    Alpha wrote:
    > Hi guys, n00bs here.
    >
    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.
    >
    > Thanks,
    > Alpha


    my $String = 'String 12345 String 67890 String whatever here';
    $string =~ s/^(String 12345 String 67890)/$1/;
     
    David Squire, Sep 8, 2006
    #2
    1. Advertising

  3. Alpha

    David Squire Guest

    David Squire wrote:
    > Alpha wrote:
    >> Hi guys, n00bs here.
    >>
    >> I was playing around with regexp and got stuck with this:
    >> $string ="String 12345 String 67890 String whatever here";
    >> basically i want to strip off the last "String" and whatever that
    >> comes after that.
    >> so $string will become "String 12345 String 67890 ";
    >>
    >> any hints will be appreciated.
    >>
    >> Thanks,
    >> Alpha

    >
    > my $String = 'String 12345 String 67890 String whatever here';
    > $string =~ s/^(String 12345 String 67890)/$1/;


    D'oh

    $String =~ s/^(String 12345 String 67890)/$1/;
     
    David Squire, Sep 8, 2006
    #3
  4. Alpha

    Matt Garrish Guest

    David Squire wrote:

    > Alpha wrote:
    > > Hi guys, n00bs here.
    > >
    > > I was playing around with regexp and got stuck with this:
    > > $string ="String 12345 String 67890 String whatever here";
    > > basically i want to strip off the last "String" and whatever that comes
    > > after that.
    > > so $string will become "String 12345 String 67890 ";
    > >
    > > any hints will be appreciated.
    > >

    >


    use strict;

    > my $String = 'String 12345 String 67890 String whatever here';
    > $string =~ s/^(String 12345 String 67890)/$1/;


    You're just substituting the matched text back in place...

    my $string = 'String 12345 String 67890 String whatever here';
    $string =~ s/^((String \d+ *)+).*/$1/;

    But you'd need to chomp the extra whitespace at the end.

    Matt
     
    Matt Garrish, Sep 8, 2006
    #4
  5. Alpha

    David Squire Guest

    Matt Garrish wrote:
    > David Squire wrote:
    >
    >> Alpha wrote:
    >>> Hi guys, n00bs here.
    >>>
    >>> I was playing around with regexp and got stuck with this:
    >>> $string ="String 12345 String 67890 String whatever here";
    >>> basically i want to strip off the last "String" and whatever that comes
    >>> after that.
    >>> so $string will become "String 12345 String 67890 ";
    >>>
    >>> any hints will be appreciated.
    >>>

    >
    > use strict;
    >
    >> my $String = 'String 12345 String 67890 String whatever here';
    >> $string =~ s/^(String 12345 String 67890)/$1/;

    >
    > You're just substituting the matched text back in place...
    >
    > my $string = 'String 12345 String 67890 String whatever here';
    > $string =~ s/^((String \d+ *)+).*/$1/;
    >
    > But you'd need to chomp the extra whitespace at the end.


    Yep. That's what I meant. Shouldn't post so late :(
     
    David Squire, Sep 8, 2006
    #5
  6. Alpha

    Matt Garrish Guest

    Matt Garrish wrote:

    > David Squire wrote:
    >
    > > Alpha wrote:
    > > > Hi guys, n00bs here.
    > > >
    > > > I was playing around with regexp and got stuck with this:
    > > > $string ="String 12345 String 67890 String whatever here";
    > > > basically i want to strip off the last "String" and whatever that comes
    > > > after that.
    > > > so $string will become "String 12345 String 67890 ";
    > > >
    > > > any hints will be appreciated.
    > > >

    > >

    >
    > use strict;
    >
    > > my $String = 'String 12345 String 67890 String whatever here';
    > > $string =~ s/^(String 12345 String 67890)/$1/;

    >
    > You're just substituting the matched text back in place...
    >
    > my $string = 'String 12345 String 67890 String whatever here';
    > $string =~ s/^((String \d+ *)+).*/$1/;

    $string =~ s/^((String \d+ )+).*/$1/;

    Doesn't really make a difference, but still a useless modiifer.

    Matt
     
    Matt Garrish, Sep 8, 2006
    #6
  7. Alpha

    John Bokma Guest

    Alpha <> wrote:

    > Hi guys, n00bs here.


    Hi n00b, you can appear less n00bish by picking a good subject line. A lot
    of questions asked here are beginners questions, no need to pick that as a
    subject, it's often obvious :-D

    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.


    Depending on your requirements, I assumed word number word number, the
    following might work:

    $string =~ s/^(\w+ \d+ \w+ \d+ ).*/$1/;

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 8, 2006
    #7
  8. Alpha

    Mumia W. Guest

    On 09/07/2006 08:25 PM, Alpha wrote:
    > Hi guys, n00bs here.
    >
    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.
    >
    > Thanks,
    > Alpha


    This is not a well-phrased question, and the subject line is
    not very descriptive, but here it goes:

    $string =~ s/ what.*//;
     
    Mumia W., Sep 8, 2006
    #8
  9. Alpha

    Mumia W. Guest

    On 09/07/2006 09:00 PM, Mumia W. wrote:
    > On 09/07/2006 08:25 PM, Alpha wrote:
    >> Hi guys, n00bs here.
    >>
    >> I was playing around with regexp and got stuck with this:
    >> $string ="String 12345 String 67890 String whatever here";
    >> basically i want to strip off the last "String" and whatever that
    >> comes after that.
    >> so $string will become "String 12345 String 67890 ";
    >>
    >> any hints will be appreciated.
    >>
    >> Thanks,
    >> Alpha

    >
    > This is not a well-phrased question, and the subject line is not very
    > descriptive, but here it goes:
    >
    > $string =~ s/ what.*//;
    >


    And that wasn't a well-phrased answer; this is:

    $string =~ s/ *String what.*//;
     
    Mumia W., Sep 8, 2006
    #9
  10. Alpha wrote:
    > Hi guys, n00bs here.
    >
    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.


    $ perl -le'
    $string = "String 12345 String 67890 String whatever here";
    $string =~ s/\D+$//;
    print $string;
    '
    String 12345 String 67890


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 8, 2006
    #10
  11. Alpha

    Xicheng Jia Guest

    Alpha wrote:
    > Hi guys, n00bs here.
    >
    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.
    >


    $string =~ s/(?!.* String).*//;

    Xicheng
     
    Xicheng Jia, Sep 8, 2006
    #11
  12. Alpha

    Alpha Guest

    Okay guys, there's a bit of misunderstanding in my part here. What I
    really want is to match a string like this:
    $str = "whatever Test bla bhlah 123123!!@#!@# Test whoever 123aojiaso
    Test i don't want to see this part in the end";

    the result will be "whatever Test bla bhlah 123123!!@#!@# Test whoever
    123aojiaso "

    so the regexp will strip off the last "Test" and whatever that comes
    after that, on a string that doesn't have a pattern except that "Test"
    will appear at least once in the document.

    If it only appears once, like here
    $str = "whatever bla bla bla bla bla Test 123combination of strings and
    !@#!@#",
    it will give me
    "whatever bla bla bla bla bla";

    in weird situation:
    $str = "Test whatever 12345";
    it will just return empty string
    and on $str = "Test Test Test whatever 12345";
    it will return "Test Test ".

    hope that makes it clear now, and thanks for the help ;)

    Regards,
    Alpha.


    Alpha wrote:
    > Hi guys, n00bs here.
    >
    > I was playing around with regexp and got stuck with this:
    > $string ="String 12345 String 67890 String whatever here";
    > basically i want to strip off the last "String" and whatever that comes
    > after that.
    > so $string will become "String 12345 String 67890 ";
    >
    > any hints will be appreciated.
    >
    > Thanks,
    > Alpha
     
    Alpha, Sep 8, 2006
    #12
  13. Alpha

    Alpha Guest

    Hey, actually this solves my problem.
    Could you please explain what you're doing here?
    Thanks.

    Ala Qumsieh wrote:
    > Alpha wrote:
    >> Hi guys, n00bs here.
    >>
    >> I was playing around with regexp and got stuck with this:
    >> $string ="String 12345 String 67890 String whatever here";
    >> basically i want to strip off the last "String" and whatever that
    >> comes after that.
    >> so $string will become "String 12345 String 67890 ";

    >
    > I don't think any of the given solutions is really what you're looking
    > for. Perhaps this is?
    >
    > $string =~ s/(.*)String.*/$1/;
    >
    > --Ala
    >
     
    Alpha, Sep 8, 2006
    #13
  14. Alpha wrote:
    > Hey, actually this solves my problem.
    > Could you please explain what you're doing here?
    > Thanks.


    Please don't top-post!

    > Ala Qumsieh wrote:
    >
    >> Alpha wrote:
    >>
    >>> Hi guys, n00bs here.
    >>>
    >>> I was playing around with regexp and got stuck with this:
    >>> $string ="String 12345 String 67890 String whatever here";
    >>> basically i want to strip off the last "String" and whatever that
    >>> comes after that.
    >>> so $string will become "String 12345 String 67890 ";

    >>
    >>
    >> I don't think any of the given solutions is really what you're looking
    >> for. Perhaps this is?
    >>
    >> $string =~ s/(.*)String.*/$1/;


    The keyword here is "greedy". Try reading up on Perl's pattern matching
    and on how patterns can be greedy.

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Sep 8, 2006
    #14
  15. Alpha wrote:
    > Okay guys, there's a bit of misunderstanding in my part here. What I
    > really want is to match a string like this:
    > $str = "whatever Test bla bhlah 123123!!@#!@# Test whoever 123aojiaso
    > Test i don't want to see this part in the end";
    >
    > the result will be "whatever Test bla bhlah 123123!!@#!@# Test whoever
    > 123aojiaso "
    >
    > so the regexp will strip off the last "Test" and whatever that comes
    > after that, on a string that doesn't have a pattern except that "Test"
    > will appear at least once in the document.
    >
    > If it only appears once, like here
    > $str = "whatever bla bla bla bla bla Test 123combination of strings and
    > !@#!@#",
    > it will give me
    > "whatever bla bla bla bla bla";
    >
    > in weird situation:
    > $str = "Test whatever 12345";
    > it will just return empty string
    > and on $str = "Test Test Test whatever 12345";
    > it will return "Test Test ".
    >
    > hope that makes it clear now, and thanks for the help ;)
    >
    > Regards,
    > Alpha.
    >
    >
    > Alpha wrote:
    >
    >> Hi guys, n00bs here.
    >>
    >> I was playing around with regexp and got stuck with this:
    >> $string ="String 12345 String 67890 String whatever here";
    >> basically i want to strip off the last "String" and whatever that
    >> comes after that.
    >> so $string will become "String 12345 String 67890 ";
    >>
    >> any hints will be appreciated.
    >>
    >> Thanks,
    >> Alpha


    Again: please don't top-post (but then, you probably haven't read my
    previous reply ;-)

    Since you don't have a fixed word you're after, I suggest you split()
    the string into (nonblank) words and then step through the resulting
    array counting the words, remembering where the word was, and then
    taking a slice of that array and join()ing it back.

    untested:

    my @words = split(' ', $str);
    my %wordcount = ();
    my %lastoccurrence = ();
    for (my $i = 0; $i <= $#words; $i++) {
    my $word = $words[$i];
    if (isaproperword($word)) { # e.g. $word =~ /^\w+$/
    $wordcount{$word}++;
    $lastoccurrence{$word} = $i;
    }
    }
    my $lastindex = @words;
    foreach (keys %wordcount) {
    next unless ($wordcount{$_} == 1);
    $lastindex = $lastoccurrence{$_} if ($lastoccurrence{$_} < $lastindex);
    }
    $lastindex = 1 if $lastindex == @words;
    $str = join(' ', @words[0..$lastindex-1];

    HTH,

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Sep 8, 2006
    #15
  16. Alpha

    Dr.Ruud Guest

    Alpha schreef:
    > Okay guys, there's a bit of misunderstanding in my part here. What I
    > really want is to match a string like this:
    > $str = "whatever Test bla bhlah 123123!!@#!@# Test whoever 123aojiaso
    > Test i don't want to see this part in the end";
    >
    > the result will be "whatever Test bla bhlah 123123!!@#!@# Test whoever
    > 123aojiaso "
    >
    > so the regexp will strip off the last "Test" and whatever that comes
    > after that, on a string that doesn't have a pattern except that "Test"
    > will appear at least once in the document.
    >
    > If it only appears once, like here
    > $str = "whatever bla bla bla bla bla Test 123combination of strings
    > and !@#!@#",
    > it will give me
    > "whatever bla bla bla bla bla";
    >
    > in weird situation:
    > $str = "Test whatever 12345";
    > it will just return empty string
    > and on $str = "Test Test Test whatever 12345";
    > it will return "Test Test ".
    >
    > hope that makes it clear now, and thanks for the help ;)


    First the keyword was 'String', now the keyword is 'Test'.

    Q1: How are you defining the keyword?

    Q2: Do you or don't you need to preserve the whitespace just before the
    last occurence of the keyword?
    (your messy examples show both)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 8, 2006
    #16
  17. Xicheng Jia wrote:

    > $string =~ s/(?!.* String).*//;


    Did you benchamark that?
     
    Brian McCauley, Sep 8, 2006
    #17
  18. Alpha

    Xicheng Jia Guest

    Brian McCauley wrote:
    > Xicheng Jia wrote:
    >
    > > $string =~ s/(?!.* String).*//;

    >
    > Did you benchamark that?


    Please be patient, I never said that it's an efficient way, just one
    possible alternative, that's it.
    --
    XC
     
    Xicheng Jia, Sep 8, 2006
    #18
    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. Alex
    Replies:
    1
    Views:
    291
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 19, 2004
  2. Mike P

    web service beginners question

    Mike P, Aug 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    745
    S. Justin Gengo
    Sep 21, 2004
  3. Roger stevenson

    Beginners Question Regarding Static Web Pages

    Roger stevenson, Apr 30, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    412
    Sam Samnah
    Jul 1, 2005
  4. Child
    Replies:
    2
    Views:
    1,029
    souri challa
    Jun 1, 2005
  5. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Late night beginners question

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Jul 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    356
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=
    Jul 31, 2005
Loading...

Share This Page