ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: how to do this regex substitution?
by botp other posts by this author
May 8 2008 8:29PM messages near this date
how to do this regex substitution? | Re: how to do this regex substitution?
From: globalrev [mailto:skanemupp@[...].se] 
# i want to replaxe the pattrn consonant o consonant, ie hoh ->  h, dad -
# >  d etc.
# so i have to check that the letter after the letter i am at is o and
# the next one is equal to the one i am at.

hi globalrev,

there are many ways.

best if you can play in irb,

"hoh" =~ /(.)o\1/
#=>  0

"testhoh" =~ /(.)o\1/
#=>  4

$1
#=>  "h"

if "testhoh".match(/(.)o\1/)
   "match!"
else
   "sorry"
end
#=>  "match!"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
  p [m,$1,$~.pre_match,$~.post_match,$~.captures]
end
["xox", "x", "qw", "tyasdfzozghj", ["x"]]
["zoz", "z", "qwxoxtyasdf", "ghj", ["z"]]
#=>  "qwtyasdfghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
  $1
end
#=>  "qwxtyasdfzghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/) do |m|
  "<#$1> "
end
#=>  "qw<x>tyasdf<z>ghj"

"qwxoxtyasdfzozghj".gsub(/(.)o\1/){"<#$1> "}
#=>  "qw<x>tyasdf<z>ghj"

your next task now is to determine whether a certain char is a consonant regardless of case.

kind regards -botp
Thread:
Globalrev
botp
Peter Jones

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved