My Asg names are creating with this name format
"digital-microservice-app1-20220627062026999600000001"
"digital-microservice-app2-20220627062026999600000001"
"digital-microservice-app3-20220627062026999600000001"
How can I search all the asg names starting with "digital-microservice-" using ruby? Is that possible to search asg names based on string?
CodePudding user response:
Input
a = ["digital-microservice-app1-20220627062026999600000001",
"digital-microservice-app2-20220627062026999600000001",
"digital-microservice-app3-20220627062026999600000001",
"digita-microservice-app3-20220627062026999600000001"
]
Code
p a.filter { |x| x.start_with?('digital-microservice') }
Output
["digital-microservice-app1-20220627062026999600000001", "digital-microservice-app2-20220627062026999600000001", "digital-microservice-app3-20220627062026999600000001"]
CodePudding user response:
Use Anchored Regular Expressions When You Need Post-Processing of Matches
String-based answers are generally faster because regular expressions are generally slower than comparable String methods. However, they offer some capabilities that would take a lot more additional parsing and transformation steps if you ever need to match something more complex than a simple prefix, or to capture elements of the match for additional processing. For example, the following Regexp does the same thing as String#start_with?:
asg_names = [
"digital-microservice-app1-20220627062026999600000001",
"digital-microservice-app2-20220627062026999600000001",
"digital-microservice-app3-20220627062026999600000001",
"analog-microservice-app4-20220627062026999600000001"
]
# this will match all your digital apps, and exclude
# the one starting with "analog"
asg_names.grep /^digital-microservice-/
#=>
["digital-microservice-app1-20220627062026999600000001",
"digital-microservice-app2-20220627062026999600000001",
"digital-microservice-app3-20220627062026999600000001"]
Example of When Regexp Helps with Post-Processing
Unlike with String methods, you can use Regexp capture groups and other features to do something with various portions of the Regexp match, which you couldn't do without additional String processing. By using named captures, pre- and post-match variables, sub-expressions, or other things not strictly needed to solve the problem as originally posted, you can simplify some things that might otherwise take a larger number of additional parsing and transformation steps. This could simplify any next steps you may have in your processing workflow.
As a trivial example, consider the following pattern that extracts the name of the app type (e.g. digital or analog) and the app name, and then transforms the resulting matches within a block into an Array of String objects suitable for logging or user-facing output:
asg_names.grep(/^digital-microservice-\K(app\d) /) do
"#{$`.tr(?-, ?\s)}found for: #{$1}".capitalize
end
#=>
["Digital microservice found for: app1",
"Digital microservice found for: app2",
"Digital microservice found for: app3"]
Go with String methods if you don't need the additional Regexp features to solve your problem, but consider regular expressions if you're doing something more complex with your matches.