Anyone know of a plugin or plugins that will do Search and Replace on layer and/or component names?

if layer.name.start_with?('F','f')
  # Replace starting F or f with F0:
  layer.name= layer.name.gsub(/\A(F|f)/,'F0')
end

OR

if instance.name.start_with?('F','f')
  # Replace starting F or f with F0:
  instance.name= instance.name.gsub(/\A(F|f)/,'F0')
end

However, regular expressions also have a case-insensitive “i” switch
(switches are placed just following the closing / of the expression.)
This can shorten the expression from: /\A(F|f)/, to: /\Af/i,
which is read as “case-insensitive match any f character at start of string”
Example:

if layer.name.start_with?('F','f')
  # Replace starting F or f with F0:
  layer.name= layer.name.gsub(/\Af/i,'F0')
end

Ruby versions prior to 2.x (SketchUp 2014 and earlier,) did not have the String#start_with?() method
so (for older SketchUp support) you may need to also use a regular expression match in the if:

if layer.name =~ /\Af/i
  # Replace starting F or f with F0:
  layer.name= layer.name.gsub(/\Af/i,'F0')
end

http://ruby-doc.org/core-2.2.4/doc/regexp_rdoc.html
http://ruby-doc.com/docs/ProgrammingRuby/html/intro.html#S5
http://ruby-doc.com/docs/ProgrammingRuby/html/language.html#UJ


# Split the string at word boundaries, iterate the array capitalizing 
# members that can be recognized as words with ASCII characters,
# and stitch the array back together assigning it back to the object:
lname= lname.split(/\b/).each{|s|s.capitalize!}.join

Ruby’s String capitalize methods will only cap lowercase characters within the ASCII set. So it’s just easier to let Ruby’s C side code handle this. (Much faster than Ruby-side string comparison in a conditional expression.)


I posted several code examples in a “Find components by name” thread, one for components, the other for groups: