Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex Question

Reply
Thread Tools

Regex Question

 
 
Mike C#
Guest
Posts: n/a
 
      04-04-2006
Hi I'm new to Perl regex's, and I've been racking my brain for a while now
on this one. Any help is appreciated. Basically, I want to return all
matches from the beginning of the string to a specific letter. As an
example, with the string "Manager" and up to the letter "a", I want to
return the following sub-groups:

M
Man

And for the word "Fishing" and up to the letter "i", I want to return:

F
Fish

Thank you in advance.

Mike C.


 
Reply With Quote
 
 
 
 
Keith Keller
Guest
Posts: n/a
 
      04-04-2006
On 2006-04-04, Mike C# <(E-Mail Removed)> wrote:
> Hi I'm new to Perl regex's, and I've been racking my brain for a while now
> on this one. Any help is appreciated. Basically, I want to return all
> matches from the beginning of the string to a specific letter. As an
> example, with the string "Manager" and up to the letter "a", I want to
> return the following sub-groups:
>
> M
> Man
>
> And for the word "Fishing" and up to the letter "i", I want to return:
>
> F
> Fish



One question, one comment.

Question: What do you do with "Manamager"?

Comment: What did you try? Please post some short code examples.

--keith

--
http://www.velocityreviews.com/forums/(E-Mail Removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-04-2006
Mike C# wrote:
> Hi I'm new to Perl regex's, and I've been racking my brain for a while now
> on this one. Any help is appreciated. Basically, I want to return all
> matches from the beginning of the string to a specific letter. As an
> example, with the string "Manager" and up to the letter "a", I want to
> return the following sub-groups:
>
> M
> Man
>
> And for the word "Fishing" and up to the letter "i", I want to return:
>
> F
> Fish


You don't need a regex for that:

$ perl -le'
$str = q[Manager];
$letter = q[a];
$len = 0;
print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
'
M
Man

$ perl -le'
$str = q[Fishing];
$letter = q[i];
$len = 0;
print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
'
F
Fish



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Xicheng
Guest
Posts: n/a
 
      04-04-2006
Mike C# wrote:
> Hi I'm new to Perl regex's, and I've been racking my brain for a while now
> on this one. Any help is appreciated. Basically, I want to return all
> matches from the beginning of the string to a specific letter. As an
> example, with the string "Manager" and up to the letter "a", I want to
> return the following sub-groups:
>
> M
> Man
>
> And for the word "Fishing" and up to the letter "i", I want to return:
>
> F
> Fish
>
> Thank you in advance.


If you want to match till the right-most instance of a specific
charater, you may use substr() and rindex(), i.e.:

my $str = substr $string, 0, rindex( $string, $letter );

or the following regex

my ($str) = $string =~ /^(.*)(?=$letter)/;

=========
#!/usr/bin/perl
use warnings;
use strict;

my %strings = ( Fishing => "i",
Banana => "a",
Manage => "a",
);

print join "\n",
map { substr $_, 0, rindex( $_, $strings{ $_ } ) }
# map { /^(.*)(?=$strings{$_})/ } # regex
keys %strings;
==============printout=============
Man
Fish
Banan
==========

Xicheng

> Mike C.


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-04-2006
Xicheng schreef:
> Mike C#:


>> Hi I'm new to Perl regex's, and I've been racking my brain for a
>> while now on this one. Any help is appreciated. Basically, I want
>> to return all matches from the beginning of the string to a specific
>> letter. [...]

>
> If you want to match till the right-most instance of a specific
> charater, you may use substr() and rindex()


Read again, he wants 'Manager' to produce both 'M' and 'Man'.

(Banana, a) -> (B, Ban, Banan)
(Mississippi, s) -> (Mi, Mis, Missi, Missis)
(Mississippi, i) -> (M, Miss, Mississ, Mississipp)

So subst + index, like John W. showed.

But what if the string starts with the special letter?

(abracadabra, a) -> (<nil>, abr, abrac, abracad, abracadabr)
(abracadabra, a) -> (abr, abrac, abracad, abracadabr)

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-04-2006
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Mike C# wrote:
> > Hi I'm new to Perl regex's, and I've been racking my brain for a while now
> > on this one. Any help is appreciated. Basically, I want to return all
> > matches from the beginning of the string to a specific letter. As an
> > example, with the string "Manager" and up to the letter "a", I want to
> > return the following sub-groups:
> >
> > M
> > Man
> >
> > And for the word "Fishing" and up to the letter "i", I want to return:
> >
> > F
> > Fish

>
> You don't need a regex for that:
>
> $ perl -le'
> $str = q[Manager];
> $letter = q[a];
> $len = 0;
> print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
> '
> M
> Man


Then again, a regex can be used:

print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-04-2006
Mike C# wrote:
> Hi I'm new to Perl regex's, and I've been racking my brain for a
> while now on this one. Any help is appreciated. Basically, I want
> to return all matches from the beginning of the string to a specific
> letter. As an example, with the string "Manager" and up to the
> letter "a", I want to return the following sub-groups:
>
> M
> Man
>
> And for the word "Fishing" and up to the letter "i", I want to return:
>
> F
> Fish
>
> Thank you in advance.


Totally different idea:

reverse() the string
while() the string still contains the letter
chop off the beginning of the string up to and including the first
occurence of that letter
the reverse() of the current string is another solution

jue


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-04-2006
Jürgen Exner <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Mike C# wrote:
> > Hi I'm new to Perl regex's, and I've been racking my brain for a
> > while now on this one. Any help is appreciated. Basically, I want
> > to return all matches from the beginning of the string to a specific
> > letter. As an example, with the string "Manager" and up to the
> > letter "a", I want to return the following sub-groups:
> >
> > M
> > Man
> >
> > And for the word "Fishing" and up to the letter "i", I want to return:
> >
> > F
> > Fish
> >
> > Thank you in advance.

>
> Totally different idea:
>
> reverse() the string
> while() the string still contains the letter
> chop off the beginning of the string up to and including the first
> occurence of that letter
> the reverse() of the current string is another solution


Why all the reversal? Chop off the letter and everything that follows
it on each step:

print "$str\n" while $str =~ s/$letter[^$letter]*$//;

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-04-2006
Anno Siegel schreef:

> print "$str\n" while $str =~ s/$letter[^$letter]*$//;


echo Bananana
| perl -nle 'print "<$1>" while /^(.*?\G.*?)a/g'

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      04-04-2006
Anno Siegel wrote:
>
> John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>>You don't need a regex for that:
>>
>>$ perl -le'
>>$str = q[Manager];
>>$letter = q[a];
>>$len = 0;
>>print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
>>'
>>M
>>Man

>
> Then again, a regex can be used:
>
> print substr($str, 0, $-[0]), "\n" while $str =~ /$letter/g;


The difference is that that prints the string '' if substr( $str, 0, 1 ) eq
$letter. If you want the same behavior with substr/index change it to:

$len = -1;
print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) >= 0;




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments