0

The title sums up my conundrum pretty well. I've been searching around the net for a while, and being new to Ruby and Regular Expressions as a whole, I'm stuck trying to figure out how to alter the case of a single word string using a RegEx "filter" such as [A-Z]([a-z]*)\b.

Basically I want the flow to be

input: woRD
filter: [A-Z]([a-z]*)\b
output: Word

I already have the words filtered into a list, so I don't need to match words; I only need to filter the case of the word using a RegEx filter.

I do not want to use standard capitalization methods, I want this to be done using Regular Expressions.

garetmckinley
  • 1,122
  • 2
  • 11
  • 29
  • 2
    Why not use 'string'.downcase.capitalize? What's wrong with using Ruby standard methods in your case? – daremkd Nov 22 '14 at 01:50
  • I want to have a list of "filters" to use to capitalize the word in various ways, such as a filter to CaSeSlIkEtHiS – garetmckinley Nov 22 '14 at 01:52
  • What is the logic of your filter? – daremkd Nov 22 '14 at 01:54
  • 2
    Aren't regular expressions only meant to *find* strings? That is, the "filter" you suggest will simply not match the input. Even if it does, it will not *change* anything. – Jongware Nov 22 '14 at 01:56
  • You will need to use SOME Ruby method in order to CONVERT a word to a case. Regular expressions are supposed to LOCATE, not CHANGE/transform things, that's what programming language logic is. Please clarify your question. – daremkd Nov 22 '14 at 01:57
  • I understand that RegEx is used to locate substrings, I just didn't know if there were any solutions for transforming strings using regex. I figured there was a module or gem somewhere to do something like this. I suppose if nothing turns up, I'll have to write my own little expression engine for transforming cases. – garetmckinley Nov 22 '14 at 01:59
  • What are your "rules" for transforming cases? Like upper-case every 2nd character? Can you provide some sample rules? – daremkd Nov 22 '14 at 02:00
  • If I were to continue with the regex approach, I'd imagine the filter would be something along the lines of ([A-Z][a-z]*) of course it'd have to have some pipes incase the word starts with a lowercase. But you get the picture. As I said, that was only an example, I just need to have a list of the filters in some sort of expression format so it's extendable. – garetmckinley Nov 22 '14 at 02:05
  • You might want to look at [words_counted](https://github.com/abitdodgy/words_counted). It's a string analyser gem I wrote. It allows you to match anything you want. It accepts a custom regex to break a string, and a regex, lambda, string, or array to filter with. If anything take a look at it's `reject` filter for an idea of how a regex can be used to filter an input. – Mohamad Nov 22 '14 at 02:16
  • So I've taken a gander and was able to implement a similar method into my module, however, but your clever trick was using the map.reject method. Which works wonders with my expression, but it's obviously only for filtering out capital/lowercase letters. Would there be a way to run the :downcase on only the matched regex? Or is this getting into the "must implement myself" type of situation. I have no problem going through that approach, I was just trying to see if there's any clever tricks I'm missing. – garetmckinley Nov 22 '14 at 03:18
  • It's really important to understand that [regular expressions are not the answer to every problem](http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/). They have their place, but capitalizing words is not one of them as the rules of capitalization are too complex to be captured in a pattern. I'd strongly recommend experimenting with [Rubular](http://rubular.com) to learn. – the Tin Man Nov 22 '14 at 06:25

3 Answers3

1

You can use

"woRD".downcase.capitalize

Ruby provides some predefined methods for these type of functionality. Try to use them instead of regex. which saves coding time!

Kranthi
  • 1,377
  • 1
  • 17
  • 34
  • FYI: `'barçelona'.upcase == "BARçELONA"`. Until there is a complete unicode string support in ruby, the reinventing wheels might be reasonable. – Aleksei Matiushkin Nov 22 '14 at 05:52
1

Well, for some reason you want to use regexps. Here you go:

# prepare hashes for gsub
to_down = (to_upper = Hash[('a'..'z').zip('A'..'Z')]).invert

# convert to downcase
downcased = 'woRD'.gsub(/[A-Z]/, to_down)
# ⇛ 'word'

titlecased = downcased.gsub(/^\w/, to_upper)
# ⇒ 'Word'

Hope it helps. Note the usage of String#gsub(re, hash) method.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

You can't use Regex to such altering as you want to do. Please read carefully this topic: How to change case of letters in string using regex in Ruby.

The best way to solve your problem is to use:

"woRD".downcase.capitalize

or

name_of_your_variable.downcase!.capitalize!

if you want to alter string in your variable permanently without need of assign it to other variable.

Community
  • 1
  • 1
PatNowak
  • 5,721
  • 1
  • 25
  • 31