How to split a CamelCase string in its substrings in Ruby?

ruby camelcase to humanize
ruby split
string tolower ruby
all caps string ruby
ruby lowercase
ruby is uppercase
toupper in ruby
ruby gsub lowercase

I have a nice CamelCase string such as ImageWideNice or ImageNarrowUgly. Now I want to break that string in its substrings, such as Image, Wide or Narrow, and Nice or Ugly.

I thought this could be solved simply by

camelCaseString =~ /(Image)((Wide)|(Narrow))((Nice)|(Ugly))/

But strangely, this will only fill $1 and $2, but not $3.

Do you have a better idea for splitting that string?

s = 'nowIsTheTime'

s.split /(?=[A-Z])/

=> ["now", "Is", "The", "Time"]

?=pattern is an example of positive lookahead. It essentially matches a point in the string right before pattern. It doesn't consume the characters, that is, it doesn't include pattern as part of the match. Another example:

    irb> 'streets'.sub /t(?=s)/, '-'
=> "stree-s"

In this case the s is matched (only the second t matches) but not replaced. Thanks to @Bryce and his regexp doc link. Bryce Anderson adds an explanation:

The?=at the beginning of the()match group is called positive lookahead, which is just a way of saying that while the regex is looking at the characters in determining whether it matches, it's not making them part of the match. split()normally eats the in-between characters, but in this case the match itself is empty, so there's nothing [there].

Python, Given a string in camel case, write a Python program to split each word in the camel case string into individual strings. Examples: Input : "GeeksForGeeks"  In its most basic form, String#split takes a single argument: the field delimiter as a string. This delimiter will be removed from the output and an array of strings split on the delimiter will be returned.

I know this is old, but worth mentioning for others who might be looking for this. In rails you could do this: "NowIsTheTime".underscore.humanize

Camel case of a given sentence, Camel case of a given sentence · Snake case of a given sentence · Split the Generate a string of size N whose each substring of size M has exactly K Given a sentence, task is to remove spaces from the sentence and rewrite in Camel case. Efficient solution : We traverse given string, while traversing we copy non​  Java method for spliting a camelcase string. This Java method accepts a camel case string, and returns a linked list of splitted strings. It has been designed to handle various kinds of different cases and fully tested and good to use.

DigitalRoss's answer is correct as it handles the general case where you do not know whether it's strict camel case (first character lower case) or Pascal case (first letter upper case).

If you know which of these forms the string is in, or you want to force one or the other, Inflector can do it.

For Pascal case:

"NowIsTheTime".titleize

For camel case:

"nowIsTheTime".titleize.camelize :lower

CamelCase - example - Progopedia, After this the string is converted to proper case (all words are lower case, Example for versions Ruby 1.9 camel3 takes advantage of the built-in libraries, a regular expression is used to split the string into boxed strings, each of these is​  Call the split method to separate strings. Use string and regular expression delimiters. Strings often contain blocks of data. With split, we separate these blocks based on a delimiter. In Ruby, a string, or a regular expression, is used as the separator. This method is widely used. When we omit an argument, it separates a string on spaces.

Event though this is a Ruby regex question and the answer by DigitalRoss is correct and shines by its simplicity, I want to add a Java answer:

// this regex doesn't work perfect with Java and other regex engines
"NowIsTheTime".split("(?=[A-Z])"); // ["", "Now", "Is", "The", "Time"]

// this regex works with first uppercase or lowercase characters
"NowIsTheTime".split("(?!(^|[a-z]|$))"); // ["Now", "Is", "The", "Time"]
"nowIsTheTime".split("(?!(^|[a-z]|$))"); // ["now", "Is", "The", "Time"]

Python Regular Expression, PHP Python Java Node.js Ruby C programming PHP Composer Laravel PHPUnit ASP. Write a Python program to find the substrings within a string. Write a Python program to separate and print the numbers of a given string. Write a python program to convert camel case string to snake case string. Divides str into substrings based on a delimiter, returning an array of these substrings. If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

Have you tried

camelCaseString =~ /(Image)(Wide|Narrow)(Nice|Ugly)/

?

Laravel helpers - Helpers - Laravel, Strings. __ class_basename e preg_replace_array Str::after Str::afterLast Str::​ascii The Arr::divide method returns two arrays, one containing the keys, and the  Shortest Palindromic Substring; Java program to print Even length words in a String; CamelCase Pattern Matching; Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P. Move all occurence of letter 'x' from the string s to the end using Recursion; Check if the given string is shuffled substring of another string; Longest palindrome formed by concatenating and reordering strings of equal length

1. Strings - Ruby Cookbook [Book], Get started with strings by typing this line into your interactive Ruby session: The String#split method takes a regular expression to use as a separator. The Facets Core library adds a String#camelcase method; it also defines the case  Replace two substrings (of a string) with each other; Number of substrings of a string; Split the string into substrings using delimiter; Lexicographical concatenation of all substrings of a string; Number of substrings of one string present in other; Count of substrings of a binary string containing K ones

Lookahead and Lookbehind Tutorial—Tips &Tricks, This lookahead asserts: at the current position in the string, what follows is the p{N})\p{Arabic} This would work in Perl, PCRE (C, PHP, R…) and Ruby 2+. feed this regex to the function that uses a regex pattern to split strings, it returns an array of words. [A-Z]) allows you to split a CamelCase word into its components. A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. In this tutorial, you'll learn how to work with strings in Ruby. You'll create strings, display them on the screen, store them in variables, join multip

String, String inflections define new methods on the String class to transform names for If a range is supplied, a substring containing characters at offsets given by the range is It raises a NameError when the name is not in CamelCase or is not initialized. Pass a string or regexp :separator to truncate text at a natural break: Java String split method is used for splitting a String into its substrings based on the given delimiter or regular expression. We have two variants of split () method in String class. String [] split (String regex): It returns an array of strings after splitting an input String based on the delimiting regular expression.

Comments
  • What would you want done with ThisIsANarrowImageOfHIV? Make a join with n, or split up HIV?
  • During my tests this regex results in ["", "Now", "Is", "The", "Time"] if the first letter is an uppercase letter. What do I wrong?
  • splash: I tried it in 1.8.7 and 1.9 and it worked on NowIsTheTime. With what language/version did you get the zero-length first element?
  • Sorry @DigitalRoss, I forgot to mention that I tested with RegexBuddy. But I wonder why this is valid in Ruby? Java also gives an empty String for the first array element: "NowIsTheTime".split("(?=[A-Z])")
  • You could always just add reject {|x| x.empty? }
  • Missing from the answer is an explanation of why it works, for people still trying to grok regular expressions. Maybe someone will find this useful: The ?= at the beginning of the () match group is called "positive lookahead," which is just a way of saying that while the regex is looking at the characters in determining whether it matches, it's not making them part of the match. split() normally eats the in-between characters, but in this case the match itself is empty, so there's nothing "in between." Regexp docs
  • Important to note, #titleize and #camelize are strictly Rails methods, and not in core Ruby.