Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match capital letters and fill in with subsequent letters until certain string length

Tags:

string

regex

ruby

I have a camel-cased string, for example: JustAString.

I would like to form strings of length 4 by following these rules:

  • grab all capital letters;
  • if more than 4 capital letters, keep only the first 4;
  • if less than 4 capital letters, capitalize and add letters that follow the last capital letter, until the length becomes 4.

Here are the 3 cases that may happen:

  • ThisIsMyString will yield TIMS (capitals);
  • ThisIsOneVeryLongString will yield TIOV (first 4 capitals);
  • MyString will yield MSTR (capitals + tr capitalized).

I managed to solve the first two cases with this snippet:

str.scan(/[A-Z]/).first(4).join

However, I am not quite sure how can I best modify the above snippet to handle the last case also (or even try something different).

P.S.: The string is guaranteed to have at least a capital and 4 characters. However, if theoretically a capital is missing, the first 4 characters should be taken into consideration. If there aren't 4 characters, the missing characters may be filled in with the first alphabet characters (abcd). But, as mentioned, these 2 edge cases won't normally happen.

like image 744
linkyndy Avatar asked Apr 29 '16 15:04

linkyndy


People also ask

How do you match a capital letter in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.

How do you test a string for capital letters?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase.

How do you split a capital letter?

To split a string on capital letters, call the split() method with the following regular expression - /(? =[A-Z])/ . The regular expression uses a positive lookahead assertion to split the string on each capital letter and returns an array of the substrings. Copied!

How do you find the uppercase of a word in Python?

Let's discuss certain ways in which only uppercase letters can be extracted from a string. List comprehension and isupper function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.


1 Answers

Replace any character(s) that precedes a capital with nothing, then grab the first 4 characters and upcase:

str.gsub(/[^A-Z]+([A-Z])/){$1}[0..3].upcase

This handles no capitals too. As for the edge case of not enough chars, you could append "abcd" but I'd find it cleaner just to do this in a separate line after the fact: output_string = (output_string + "abcd")[0..3] if output_string.length < 4. This is reads much cleaner and performs (inconsequentially) better if this is really a rare edge case.

like image 79
Andrew Schwartz Avatar answered Nov 08 '22 03:11

Andrew Schwartz