-1

Possible Duplicate:
Regular expression to identify CamelCased words

I'm trying to detect CamelCase in a text. In Rubular, my regex works fine, but doesn't seems to work in my script. My regex: http://rubular.com/r/fbeXAM69dX

My script: recognizers.rb

class NameRecognizer
def recognize(text)
  names = text.scan(NameRegex)
  th = TagHelper.new
  cc = th.isCamelCase?(names)
  cc ? (puts "Camel Case detected in '#{text}'") : (puts "No camel case in '#{text}'")
end
end

In another document, tag_helpers.rb

class TagHelper
def isCamelCase?(value)
  value = value.join(' ')
  finder = /([A-Z][a-z]+[a-z][A-Z][a-zA-Z]+)/
  value.match(finder) ? bool = true : bool = false
end
end

isCamelCase? worked but my regex detected something wrong, so I had to change, so I know the problem i my regex. If somebody have an idea, I'm kinda desperate.

EDIT: Here is my test

Recognizers::NameRecognizer.new.recognize("Camel Case")
Recognizers::NameRecognizer.new.recognize("NewsItalie")

I'm using Ruby 1.8.7

Community
  • 1
  • 1
Simon
  • 619
  • 2
  • 9
  • 23
  • Your question is not clear because you introduce several constants without explanation. How are you expecting people to understand this? – sawa Jan 13 '12 at 18:45
  • @Sawa I'll edit this to make it clearer. – Simon Jan 13 '12 at 18:58

2 Answers2

1

Your regex should be /([A-Z][a-z]+[A-Z][a-zA-Z]+)/, the second [a-z] was unecessary and would exclude matches like "RoBot".

Jason Lewis
  • 1,314
  • 8
  • 13
  • 1
    I got an error with the g floag, like unknown regexp option - g (SyntaxError) – Simon Jan 13 '12 at 18:40
  • 4
    the global flag doesn't exist in ruby, use `scan` instead http://ruby-doc.org/core-1.9.3/String.html#method-i-scan @Jason +1 for improving the regex – ian Jan 13 '12 at 18:43
  • It works but detect a word like "Camel Case" too and shouldn't – Simon Jan 13 '12 at 18:49
  • @Iain you're absolutely right... I'll edit that. – Jason Lewis Jan 13 '12 at 18:56
  • @Simon odd, I'm not seeing that. I'm just doin a one-liner `puts ARGV.join(' ').match /([A-Z][a-z]+[A-Z][a-zA-Z]+)/` and passing it test cases... it matches "CamelCase" but not "Camel Case". – Jason Lewis Jan 13 '12 at 19:03
  • 1
    @Simon you might've got some stray whitespace in there, add the 'x' flag to the end of the regex to indicate that you'll be explicit about any whitespace you wish to match (I do it for almost all regex) e.g. `/([A-Z][a-z]+[A-Z][a-zA-Z]+)/x` – ian Jan 13 '12 at 19:05
  • Yeah, the regex works great in rubular. With my test, with match(/regex/), it doesn't match anything, with scan(/regex/), it match everything – Simon Jan 13 '12 at 19:07
1

As an addendum to Jason's answer, mainly because the comments compact everything:

The pattern:

r = /^[A-Z]\w+(?:[A-Z]\w+){1,}/x

Test data:

strs = ["Camelcase", "CamelCase", "camelCase", "camel case", "Camel Case", "Camel case", "CamelCaseLong"]

The code:

strs.each{|s| p s.scan( r ) }

Results:

[]
["CamelCase"]
[]
[]
[]
[]
["CamelCaseLong"]
ian
  • 12,003
  • 9
  • 51
  • 107