Regex Question

M

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

Keith Keller

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
 
J

John W. Krahn

Mike said:
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;
$len = 0;
print substr $str, 0, $len while ( $len = index $str, $letter, $len + 1 ) > 0;
'
F
Fish



John
 
X

Xicheng

Mike said:
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
 
D

Dr.Ruud

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

Anno Siegel

John W. Krahn said:
Mike said:
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
 
J

Jürgen Exner

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

Anno Siegel

Jürgen Exner said:
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
 
J

John W. Krahn

Anno said:
John W. Krahn said:
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
 
X

Xicheng

Dr.Ruud said:
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)

ou, I did not read it carefully, but it might still be done some other
ways:

print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

Xicheng Jia :)
====
 
X

Xicheng

Dr.Ruud said:
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)

ou, I did not read it carefully, but it might still be done some other
ways:

print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

Xicheng Jia :)
====
 
X

Xicheng

Dr.Ruud said:
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.

dr => But what if the string starts with the special letter?

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

For these kinds of cases:

perl -le '$_="iiMississippi"; print $x.=$1 while /(^|.+?)(?=i)/g'

======

i
iiM
iiMiss
iiMississ
iiMississipp
=============

Xicheng
 
D

Dr.Ruud

Xicheng schreef:

[ (aBanana, a) -> (aB, aBan, aBanan) ]

print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

Yes, nice indeed. The \G is not necessary, as you showed with

print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'

(slightly reformatted)
 
I

it_says_BALLS_on_your forehead

Dr.Ruud said:
Xicheng schreef:

[ (aBanana, a) -> (aB, aBan, aBanan) ]

print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

Yes, nice indeed. The \G is not necessary, as you showed with

print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'

should the above be:

s/ing//;
 
D

Dr.Ruud

it_says_BALLS_on_your forehead schreef:
Dr.Ruud:
Xicheng:
[ (aBanana, a) -> (aB, aBan, aBanan) ]

print $str .= $1 while $string =~ /\G(.+?)(?=$letter)/g;

Yes, nice indeed. The \G is not necessary, as you showed with

print $str .= $1 while $string =~ /(^|.+?)(?=$letter)/g'

should the above be:

s/ing//;

No, it was implied that $string contains something like 'Mississippi'
and $letter for example 'i'.
The $str collects the captured strings.

echo "a banana"
| perl -nle 'print "<", $str .= $1, ">" while /(^|.+?)(?=a)/g'
 
D

Dr.Ruud

(e-mail address removed) schreef:
Anno Siegel:
Mike C#:
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

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

print $`, "\n" while $str =~ /$letter/g;

Of course, and to me the nicest so far.

Test:
echo abracadabra | perl -nle 'print "<$`>" while /a/g'
 
M

Mike C#

Thank you all for your help on this. Your regular expression examples
worked great. I'm pretty familiar with substr functions, looping
structures, iterators, arrays, etc., etc. This was all about the regex's.

To answer the question why would I want to do this? More out of curiousity
than anything. As I mentioned, I'm learning regex's and I'm finding out
it's more art than science. I was focusing on the ?: and ?= operators but
getting nowhere fast. Basically I was looking for a more "elegant" way
other than looping.

Thanks!
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top