简体   繁体   English

Ruby gsub检查或(||)模式

[英]Ruby gsub check or (||) pattern

Ruby Experts, Ruby专家,

I'm trying to check and remove string from multiple files under a folder and below is my code snippet, 我正在尝试检查并删除文件夹下多个文件中的字符串,以下是我的代码段,

........
work_files = Dir.glob("E:\nano\*_access.txt")

val=File.open('E\new_dir\access_list.txt').read
val.each_line do |line|
    line.chomp!
    work_files.each do |file_name|
      text = File.read(file_name)
      new_content = text.gsub(( /#{Regexp.escape(line)}\,\s/, '')  || ( /#{Regexp.escape(line)}\n/, '' )) 
..........
........ 

from one of the files,like below I'm checking for the string ncr\\atxe5t (can occur multiple times also). 从其中一个文件中,如下所示,我正在检查字符串ncr \\ atxe5t (也可能出现多次)。 its getting removed from the last line for the pattern (/#{Regexp.escape(line)}\\,\\s/, '') but its not getting removed from the last but one line for the pattern - /#{Regexp.escape(line)}\\n/, '' . 它从模式(/#{Regexp.escape(line)}\\,\\s/,'')的最后一行中删除,但没有从模式的最后一行-/#{Regexp。中删除。 escape(line)} \\ n /,''。

I;m getting the error like below 我得到如下错误

... ^ test.rb:14: syntax error, unexpected ',', expecting ')' ...| ... ^ test.rb:14:语法错误,意外的',',期望的是')'... | ( /#{Regexp.escape(line)}\\n/, '' )) (/#{Regexp.escape(line)}\\n/,``))

Eg. 例如。 E:\\nano\\ids_access.txt E:\\ nano \\ ids_access.txt

# Digital created on July 2016
# Digital Owner: John Cena (jxcgo)
# Digital access: create delete access

[pool]
@rem = ncr\abc_efg_dev, ncr\abc_efg_test, ncr\jx8go5, ncr\atxe5t
@digital_owner = ncr\atxe5t, ncr\yui30n, ncr\bhyrl4

I'm not sure whether or (||) condition pattern is allowed in gsub method but I want to make use of the gsub method only. 我不确定gsub方法中是否允许或(||)条件模式,但我只想使用gsub方法。 is it possible in ruby. 红宝石有可能吗? Thanks in advance. 提前致谢。 Hope I have explained clearly. 希望我已经解释清楚了。

Nope, || 不, || is not usable in method args. 在方法args中不可用。 I think that's what you want to do: gsub(/#{Regexp.escape(line)}\\,(\\s|\\Z)/, '') . 我认为这就是您想要执行的操作: gsub(/#{Regexp.escape(line)}\\,(\\s|\\Z)/, '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM