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

  2. Alpha

    David Squire Guest

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

  3. Alpha

    David Squire Guest

    D'oh

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

    Matt Garrish Guest

    use strict;
    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

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

    Matt Garrish Guest

    $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

    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
    Depending on your requirements, I assumed word number word number, the
    following might work:

    $string =~ s/^(\w+ \d+ \w+ \d+ ).*/$1/;
     
    John Bokma, Sep 8, 2006
    #7
  8. Alpha

    Mumia W. Guest

    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

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

    $string =~ s/ *String what.*//;
     
    Mumia W., Sep 8, 2006
    #9
  10. $ perl -le'
    $string = "String 12345 String 67890 String whatever here";
    $string =~ s/\D+$//;
    print $string;
    '
    String 12345 String 67890


    John
     
    John W. Krahn, Sep 8, 2006
    #10
  11. Alpha

    Xicheng Jia Guest

    $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 [email protected]#[email protected]# Test whoever 123aojiaso
    Test i don't want to see this part in the end";

    the result will be "whatever Test bla bhlah [email protected]#[email protected]# 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
    [email protected]#[email protected]#",
    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, Sep 8, 2006
    #12
  13. Alpha

    Alpha Guest

    Hey, actually this solves my problem.
    Could you please explain what you're doing here?
    Thanks.
     
    Alpha, Sep 8, 2006
    #13
  14. Please don't top-post!

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

    Josef
     
    Josef Moellers, Sep 8, 2006
    #14
  15. 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 Moellers, Sep 8, 2006
    #15
  16. Alpha

    Dr.Ruud Guest

    Alpha schreef:
    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)
     
    Dr.Ruud, Sep 8, 2006
    #16
  17. Did you benchamark that?
     
    Brian McCauley, Sep 8, 2006
    #17
  18. Alpha

    Xicheng Jia Guest

    Please be patient, I never said that it's an efficient way, just one
    possible alternative, that's it.
     
    Xicheng Jia, Sep 8, 2006
    #18
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.