regex - Ruby gsub case insensitive not working as expected -


i have problem regex using gsub. part of program problematic loads 2 text files: 1 work on, , 1 array of abbreviations. thing want delete \ns directly before or directly after of abbreviations (i keep them separate on purpose). not problem , solution works, abbreviations might in upper or lower case, while list contains 1 version of each (mixed lower , upper case).

the function works fine, except upper/lower case issue. added ::ignorecase regex in gsub, doesn't seem change anything. below function gets array of strings:

def generateregexes(some_array)     matchregexprefix = regexp.new("\n(%{abbrevreplace})" % {         abbrevreplace: regexp.union(some_array)     }, regexp::ignorecase)     matchregexsuffix = regexp.new("(%{abbrevreplace})\n" % {         abbrevreplace: regexp.union(some_array)     }, regexp::ignorecase)     return [matchregexprefix, matchregexsuffix] end 

and here how call function:

regexabbrev = generateregexes listofabbreviations stringtobefixed.gsub!(regexabbrev [0], " \\1") stringtobefixed.gsub!(regexabbrev [1], "\\1 ") 

what happen method match \nabbrev or abbrev\n regardless of case , replace \n space while keeping abbreviation in original form.


edit: supplying requested information (don't mind polish language gibberish).

portion of input:

fum, u, lm. y 1. † dym, wyziew, para: fumy głowy z picia biją. troc. 2. † pachnidło, perfumy, kosmetyk. zdr. † fumik. <Łć. fumus> × fungować, uje, ował sprawować urząd, wypełniać obowiązki: funguje jako poseł królewski. szuj. <Łć. fungi> funkcja, i, lm. e 1. sprawa załatwiania. zdr. funkcyjka. <Łć. functio> 

portion of abbreviation list:

troc. szuj. zdr. 

expected output:

fum, u, lm. y 1. † dym, wyziew, para: fumy głowy z picia biją. troc. 2. † pachnidło, perfumy, kosmetyk. zdr. † fumik. <Łć. fumus> × fungować, uje, ował sprawować urząd, wypełniać obowiązki: funguje jako poseł królewski. szuj. <Łć. fungi> funkcja, i, lm. e 1. sprawa załatwiania. zdr. funkcyjka. <Łć. functio> 

the output (half of want):

fum, u, lm. y 1. † dym, wyziew, para: fumy głowy z picia biją. troc. 2. † pachnidło, perfumy, kosmetyk. zdr. † fumik. <Łć. fumus> × fungować, uje, ował sprawować urząd, wypełniać obowiązki: funguje jako poseł królewski. szuj. <Łć. fungi> funkcja, i, lm. e 1. sprawa załatwiania. zdr. funkcyjka. <Łć. functio> 

another thing can instead of using regexp.union manually construct regexp source:

pattern = some_array.map(&regexp.method(:escape)).join('|') text.gsub(/\n(#{pattern})/i, ' \1').gsub(/(#{pattern})\n/i, '\1 ') 

output:

fum, u, lm. y 1. † dym, wyziew, para: fumy głowy z picia biją. troc. 2. † pachnidło, perfumy, kosmetyk. zdr. † fumik. <Łć. fumus> × fungować, uje, ował sprawować urząd, wypełniać obowiązki: funguje jako poseł królewski. szuj. <Łć. fungi> funkcja, i, lm. e 1. sprawa załatwiania. zdr. funkcyjka. <Łć. functio> 

Comments