beginners question

A

Alpha

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
 
D

David Squire

Alpha said:
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

David Squire

David said:
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/;
 
M

Matt Garrish

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
 
D

David Squire

Matt said:
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.

Yep. That's what I meant. Shouldn't post so late :(
 
M

Matt Garrish

Matt said:
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/;
$string =~ s/^((String \d+ )+).*/$1/;

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

Matt
 
J

John Bokma

Alpha said:
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/;
 
M

Mumia W.

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.*//;
 
M

Mumia W.

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.*//;
 
J

John W. Krahn

Alpha said:
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
 
X

Xicheng Jia

Alpha said:
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
 
A

Alpha

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

Alpha

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

Josef Moellers

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

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
 
J

Josef Moellers

Alpha said:
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.

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
 
D

Dr.Ruud

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)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top