Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下字符串:
1) Beach Cottage (CP) 2) Beach Cottage (AP) 3) Cotta (GAP)
我想( )从一开始就在这之间进行替换CP
( )
CP
试试这个例如:
str = "Beach Cottage (CP)" str.match(/(\((.*)\))/)[2]
您也可以将 scan 与 regx 一起使用:
str = "Beach Cottage (CP)" needed_sub_str = str.scan(/\((.*)\)/) puts "expected sub string :: #{needed_sub_str}"
输出 ::
expected sub string :: CP