result = subject.gsub(/\b(?!1\b)\d+/, '5')
Объяснение:
\b # match at a word boundary (in this case, at the start of a number)
(?! # assert that it's not possible to match
1 # 1
\b # if followed by a word boundary (= end of the number)
) # end of lookahead assertion
\d+ # match any (integer) number
Редактировать:
Если вы просто хотите заменить числа, окруженные <m>
и </m>
,Вы можете использовать
result = subject.gsub(/<m>(?!1\b)\d+<\/m>/, '<m>5</m>')