CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Regex Problem

 



zapzap
User

Nov 11, 2013, 6:11 PM

Post #1 of 7 (1434 views)
Regex Problem Can't Post

Greetings,

Suppose we have the string (all lower case) and we want to change any consanant (! aeiou) to double itself with an 'o' in between. I want to create a subroutine such that

translate('this is fun') returns
tothohisos isos fofunon

I tried a couple of things but didn't quite work:

Code
$s = 'this is fun' 
$s =~ s/([^aeiou])/$1o$1/g;
but it changed the spaces to ' o '
Not exactly what I wanted
And it has to be in a subroutine


Help?
TIA


FishMonger
Veteran / Moderator

Nov 12, 2013, 6:39 AM

Post #2 of 7 (1422 views)
Re: [zapzap] Regex Problem [In reply to] Can't Post

If consonants are what you need to match, then it seems clear that's what you need to specify in the regex character class.

Since this is obviously a class homework assignment, I can't/won't post a complete solution, but I can give you some guidance.

Please post your complete subroutine code and a specific question on the part that is giving you trouble. Don't forget to fix your regex based on my above comment.


(This post was edited by FishMonger on Nov 12, 2013, 6:39 AM)


Laurent_R
Veteran / Moderator

Nov 12, 2013, 10:47 AM

Post #3 of 7 (1414 views)
Re: [zapzap] Regex Problem [In reply to] Can't Post

To remove the problem with the space being matched, you might add a space character to your character class:


Code
[^aeiou ]


but that will not solve the problem you will also meet with punctuation characters, parens, possibly non English characters, etc. You might be better off defining the consonant chartacter class directly, such as:

Code
[b-df-hj-n ...]



zapzap
User

Nov 12, 2013, 10:51 AM

Post #4 of 7 (1414 views)
Re: [FishMonger] Regex Problem [In reply to] Can't Post

I'm not sure how you can ascertain that this is 'obviously' a homework assignment. You are wrong.


zapzap
User

Nov 12, 2013, 11:01 AM

Post #5 of 7 (1411 views)
Re: [Laurent_R] Regex Problem [In reply to] Can't Post

Duh, that was awesome! I can't believe I didn't think of that, good job.


Code
 
sub change {
my $str = shift;
my $newstr;
for my $char (split(//,$str)) {
if ( $char =~ /[[:alpha:]]/ and $char =~ /[^aeiou]/) {
$char =~ s/([^aeiou])/$1o$1/;
}
$newstr .= $char;
}
print $newstr,"\n";
}


change('some long string');



And what if we tried


Code
[^aeiou [:punct]] or [^aeiou\s\W] ???


Thank you!

And again, this ain't a homework assignment.
If it was, I don't know why it would be wrong to ask for help.
This is not a homework assignment.

Thank again
zap


FishMonger
Veteran / Moderator

Nov 12, 2013, 11:21 AM

Post #6 of 7 (1407 views)
Re: [zapzap] Regex Problem [In reply to] Can't Post

Ok, I'll take your word that it's not a homework assignment.

After working in perl forums for a number of years you can get a pretty good (but not always accurate) feel by the way the question is worded and/or by the code about whether or not it's a homework assignment. Yours gave me that impression.

You're jumping through too many hoops to get the job done.


Code
sub change { 
$_[0] =~ s/([bcdfghjk-np-tv-z])/$1o$1/g;
return $_[0];
}



(This post was edited by FishMonger on Nov 12, 2013, 11:26 AM)


zapzap
User

Nov 12, 2013, 3:10 PM

Post #7 of 7 (1397 views)
Re: [FishMonger] Regex Problem [In reply to] Can't Post

Nice, good stuff.

Thanx to everyone that responded

zap

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives