Project1

标题: [好人求好报]字串匹配转换请益 [打印本页]

作者: alexncf125    时间: 2020-10-12 10:23
标题: [好人求好报]字串匹配转换请益
本帖最后由 alexncf125 于 2020-10-12 11:02 编辑

"\i[123]\c[4]技能"
我想icon = 123, color = 4,要怎写?
  1.       color = #####怎写?
  2.       sprite.bitmap.font.color.set(text_color(color))
  3.       
  4.       bitmap = Cache.system("Iconset")
  5.       icon = string.gsub(/[\D]/, '').to_i #####怎改?
  6.       icon_rect = Rect.new(icon % 16 * 24, icon / 16 * 24, 24, 24)
  7.       sprite.bitmap.blt(0, 0, bitmap, icon_rect)
  8.       
  9.       string = string.gsub(/\\C\[(\d+)\]/i,'')
  10.       string = string.gsub(/\\I\[(\d+)\]/i,'')
  11.       
  12.       if icon > 0
  13.         sprite.bitmap.draw_text(24, 0, rect.width, rect.height, string, alignment)
  14.         @ms_command_slider << [sprite, offset_x, offset_y, target_x + 12, index, 0,
  15.                                sprite.bitmap.text_size(string).width > self.width]
  16.         return
  17.       end
复制代码

作者: 灯笼菜刀王    时间: 2020-10-12 11:55
本帖最后由 灯笼菜刀王 于 2020-10-12 12:13 编辑

a = "\\i[123]\\c[4]技能"
a =~ /\\i\[(\d+)\]/        #这样是只能匹配小写i
icon = $1.to_i
a =~ /\\[Cc]\[(\d+)\]/        #这样是大写小写c都可以匹配 当然写成 a =~ /\\c\[(\d+)\]/i 也可以
color = $1.to_i

p icon,color

注意在脚本里 字符串要表示"\" 本身, 要写成"\\"

其实你这段可以这样写, 省得重复匹配

str = string.dup
str.gsub!(/\\[Cc]\[\(\d+)\]/) {""}
color = $1.to_i
str.gsub!(/\\[Ii]\[\(\d+)\]/) {""}
icon = $1.to_i

p str,icon,color
作者: alexncf125    时间: 2020-10-12 14:50
本帖最后由 alexncf125 于 2020-10-12 15:47 编辑
灯笼菜刀王 发表于 2020-10-12 11:55
a = "\\i[123]\\c[4]技能"
a =~ /\\i\[(\d+)\]/        #这样是只能匹配小写i
icon = $1.to_i


已解決
大大不行喔好像是多了些\号





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1