How to catch first occurence of [a-z]

Hi,

Yes, I’m in learning mode and trying to see how I can change very first
occurrence of alpha char in string (or list), in my example first char
in string could be \s or \t, so I need to check for this.
It’s multi line string, so I broke up it in list elements = line, and
now thinking how to process it.
What string function I can use?

list = multi_linestring.split(/\n/)
##then iterate thru each line { |ll| …???}

Thanks all

On Thu, Feb 9, 2012 at 6:07 AM, Mario T. [email protected] wrote:

list = multi_linestring.split(/\n/)
##then iterate thru each line { |ll| …???}

Thanks all

Writing an example is shorter than a long explanation. As
your learning experience, try to understand in detail how/why
this works …

$ irb
1.9.3p0 :001 > " \t \t first line \nsecond line".split(/\n/).each
do
|line|
1.9.3p0 :002 > line.sub!(/[a-zA-Z]/, ‘*’)
1.9.3p0 :003?> end
=> [" \t \t *irst line ", “*econd line”]

Hints:

  • check the difference between sub , sub! , gsub , gsub!
  • check character classes in regular expressions

HTH,

Peter

*** Available for a new project ***

Peter V.
http://twitter.com/peter_v
http://rails.vandenabeele.com
http://coderwall.com/peter_v

Thanks, Peter !
for your example and resources.

That worked, I actually need now to capitalize each alpha char, trying
to edit your code and looks like I still need to use . I
can’t do block [A-Z] in target sub.

dai

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs