Extract a string starting with a capital letter with a regular expression (Ruby)

What to do this time

As the title suggests, it extracts a string that starts with a capital letter in a regular expression.

code

The code I wrote this time is as follows


#A method to extract a string starting with a capital letter from an array
def upperstr(array)

  #Variables used as array subscripts
  count = 0

  #An array that stores strings starting with an uppercase letter
  upper = []

  #Extract all the elements contained in the array with each method
  array.each{|arraystr|

    #Extracts strings that start with a capital letter in a regular expression and arranges them(upper)Store in
    upper[count] = arraystr.slice(/^[A-Z].*/)

    #Add 1 to the array subscript
    count += 1
  }
  
  #If the beginning of the character string is lowercase, nil is stored in the array, so delete nil with the delete method.
  upper.delete(nil)

  #As the return value of the method, change the array that stores the character string starting with a capital letter.
  return upper

end


#User's guide
p "Enter the character strings separated by a space."

#Assign a string entered from the console to a variable
str = gets

#Separate the input character string with a space and assign it to the array
ary = str.split(" ")

#Assign the return value of the upperstr method to a variable
# (The upperstr method is a method that extracts a string starting with an uppercase letter.)
upperary = upperstr(ary)

#User's guide
p "I took out a string starting with a capital letter"

#Extract all the elements contained in the array with each method
upperary.each{|ustr|
  #Show the contents of the array
  p ustr
}

Regular expressions

① Code upper[count] = arraystr.slice(/[1].*/)

②/ The range surrounded by / and / is the range of the regular expression pattern.

③^ Indicates that the character immediately after ^ is the first character

④[A-Z] It means A to Z, which means it is capitalized. You can search for a character string that starts with an uppercase letter by typing ^ [A-Z]. If you want to make it lowercase, use [a-z].

⑤. . Is any one character.

⑥*

Operation check

I will write the result of actually running the program. The input value is "aaa AAA B b CC cc DDDDD ddddd". It retrieves strings that start with an uppercase letter, so the expected result is "AAA B CC DDDDD".

% ruby regex.rb 
"Enter the character strings separated by a space."
aaa AAA B b CC cc DDDDD ddddd
"I took out a string starting with a capital letter"
"AAA"
"B"
"CC"
"DDDDD"

The result was as expected! That's all for this article!


  1. A-Z ↩︎

Recommended Posts

Extract a string starting with a capital letter with a regular expression (Ruby)
Extract a part of a string with Ruby
Ruby Regular Expression Extracts from a specific string to a string
I want to extract between character strings with a regular expression
Ruby regular expression
[Java] Cut out a part of the character string with Matcher and regular expression
Extract elements by doing regular expression replacement from a lot of HTML with java
Ruby Learning # 8 Working With String
Cut out a Ruby string
Replace with a value according to the match with a Java regular expression
A little regular expression story Part 1
A little regular expression story Part 2
Make a typing game with ruby
Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
<java> Split the address before and after the street address with a regular expression
Apply regular expression matching with JSON Sassert
Ruby: Regular expression summary * Code sample available
Let's make a smart home with Ruby!
I made a risky die with Ruby
Split a string with ". (Dot)" in Java
Execute method on match result with #Ruby regular expression replacement (Example: Uppercase letters)